Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. class Solution {
  2.  
  3. public String solution(String S) {
  4. String [] files = S.split("\n");
  5. int l = files.length;
  6. String []fName =new String[l];
  7. String []fExt =new String[l];
  8. String []fTime= new String[l];
  9. String []result =new String[l];
  10. Photo[]fileList =new Photo[l];
  11.  
  12. // Loop through the file list and store in corresponding arrays and an array-like object
  13. for(int i=0; i<files.length; i++){
  14. String file = files[i];
  15. fName[i] = file.substring(file.indexOf(",") + 2, file.lastIndexOf(","));
  16. fExt[i] = file.substring(file.indexOf("."), file.indexOf(","));
  17. fTime[i] = file.substring(file.lastIndexOf(",") + 2);
  18. fileList[i] = new Photo(fName[i],fExt[i],fTime[i],""+i);
  19. }
  20.  
  21. // Find the list of places (unique) and collect entries and corresponding timestamps for each place
  22. Set<String> placesSet = new HashSet<String>(Arrays.asList(fName));
  23. String [] places = new String[placesSet.size()];
  24. int i = 0;
  25. for (String p : placesSet){
  26. places[i++] = p;
  27. }
  28. for(int j=0; j<places.length; j++){
  29. List<Integer> fiList = new ArrayList<Integer>();
  30. List<Date> tStamp = new ArrayList<Date>();
  31. for(int k=0; k<fileList.length; k++){
  32. if(fileList[k].name == places[j]){
  33. fiList.set(fiList.size(), k);
  34. tStamp.set(tStamp.size() ,getDateTime(fTime[k]));
  35. }
  36. }
  37.  
  38. // Sort based on timestamp and update 'position' value (with prefix, if required) based on resulting order
  39. Collections.sort(tStamp);
  40. int position = 1;
  41. String prefix = (fiList.size() >= 100 ? "100" : (fiList.size() >= 10 ? "10" : ""));
  42.  
  43. for(int d=0; d<fiList.size(); d++){
  44. for(int m=0; m<tStamp.size(); m++){
  45. if(fileList[fiList.get(d)].timestamp == tStamp.get(m)){
  46. fileList[fiList.get(d)].position = (prefix.equals("10") ? ((m+1) < 10 ? "0" : "") : (prefix.equals("100") ? ((m+1) < 100 ? "0" : "") : "")) + (m + 1);
  47. }
  48. }
  49. }
  50. }
  51.  
  52. // Loop through the file list and store in the new format
  53. for(int n=0; n<fileList.length; n++){
  54. result[n] = fileList[n].name + fileList[n].position + fileList[n].extension;
  55. }
  56. return arrayToString(result);
  57. }
  58. public static String arrayToString(String array[])
  59. {
  60. if (array.length == 0) return "";
  61. StringBuilder sb = new StringBuilder();
  62. for (int i = 0; i < array.length; ++i)
  63. {
  64. sb.append(",'").append(array[i]).append("'");
  65. }
  66. return sb.substring(1);
  67. }
  68.  
  69. public Date getDateTime(String dateString) {
  70. try {
  71. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  72. return dateFormat.parse(dateString);
  73. }
  74. catch (Exception e) {
  75. return null;
  76. }
  77. }
  78. }
  79.  
  80. class Photo implements Comparable<Photo> {
  81.  
  82. public String name;
  83. public String position;
  84. public String DesiredPosition;
  85. public String FinalName;
  86. public String extension;
  87. public String City;
  88. public Date timestamp;
  89. public Date DateTime;
  90. public int compareTo(Photo other) {
  91. if ((other == null)) {
  92. return 1;
  93. }
  94. else {
  95. return this.DateTime.compareTo(other.DateTime);
  96. }
  97.  
  98. }
  99.  
  100. public Photo(String name, String extension, String timestamp, String position) {
  101. this.name = name;
  102. this.extension = extension;
  103. this.timestamp = getDateTime(timestamp);
  104. //this.DateTime = getDateTime(timestamp);
  105. this.position = position;
  106. }
  107. public Date getDateTime(String dateString) {
  108. try {
  109. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  110. return dateFormat.parse(dateString);
  111. }
  112. catch (Exception e) {
  113. return null;
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement