Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public String solution(String S) {
- String [] files = S.split("\n");
- int l = files.length;
- String []fName =new String[l];
- String []fExt =new String[l];
- String []fTime= new String[l];
- String []result =new String[l];
- Photo[]fileList =new Photo[l];
- // Loop through the file list and store in corresponding arrays and an array-like object
- for(int i=0; i<files.length; i++){
- String file = files[i];
- fName[i] = file.substring(file.indexOf(",") + 2, file.lastIndexOf(","));
- fExt[i] = file.substring(file.indexOf("."), file.indexOf(","));
- fTime[i] = file.substring(file.lastIndexOf(",") + 2);
- fileList[i] = new Photo(fName[i],fExt[i],fTime[i],""+i);
- }
- // Find the list of places (unique) and collect entries and corresponding timestamps for each place
- Set<String> placesSet = new HashSet<String>(Arrays.asList(fName));
- String [] places = new String[placesSet.size()];
- int i = 0;
- for (String p : placesSet){
- places[i++] = p;
- }
- for(int j=0; j<places.length; j++){
- List<Integer> fiList = new ArrayList<Integer>();
- List<Date> tStamp = new ArrayList<Date>();
- for(int k=0; k<fileList.length; k++){
- if(fileList[k].name == places[j]){
- fiList.set(fiList.size(), k);
- tStamp.set(tStamp.size() ,getDateTime(fTime[k]));
- }
- }
- // Sort based on timestamp and update 'position' value (with prefix, if required) based on resulting order
- Collections.sort(tStamp);
- int position = 1;
- String prefix = (fiList.size() >= 100 ? "100" : (fiList.size() >= 10 ? "10" : ""));
- for(int d=0; d<fiList.size(); d++){
- for(int m=0; m<tStamp.size(); m++){
- if(fileList[fiList.get(d)].timestamp == tStamp.get(m)){
- fileList[fiList.get(d)].position = (prefix.equals("10") ? ((m+1) < 10 ? "0" : "") : (prefix.equals("100") ? ((m+1) < 100 ? "0" : "") : "")) + (m + 1);
- }
- }
- }
- }
- // Loop through the file list and store in the new format
- for(int n=0; n<fileList.length; n++){
- result[n] = fileList[n].name + fileList[n].position + fileList[n].extension;
- }
- return arrayToString(result);
- }
- public static String arrayToString(String array[])
- {
- if (array.length == 0) return "";
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < array.length; ++i)
- {
- sb.append(",'").append(array[i]).append("'");
- }
- return sb.substring(1);
- }
- public Date getDateTime(String dateString) {
- try {
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- return dateFormat.parse(dateString);
- }
- catch (Exception e) {
- return null;
- }
- }
- }
- class Photo implements Comparable<Photo> {
- public String name;
- public String position;
- public String DesiredPosition;
- public String FinalName;
- public String extension;
- public String City;
- public Date timestamp;
- public Date DateTime;
- public int compareTo(Photo other) {
- if ((other == null)) {
- return 1;
- }
- else {
- return this.DateTime.compareTo(other.DateTime);
- }
- }
- public Photo(String name, String extension, String timestamp, String position) {
- this.name = name;
- this.extension = extension;
- this.timestamp = getDateTime(timestamp);
- //this.DateTime = getDateTime(timestamp);
- this.position = position;
- }
- public Date getDateTime(String dateString) {
- try {
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- return dateFormat.parse(dateString);
- }
- catch (Exception e) {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement