cyb
BAN USER
- 3of 3 votes
AnswersGive a path get it's canonical form. So for example if you have path in the form e/../../a/d/./../b/c then you should return a/b/c.
I have the solution but it's not the most optimal or the best solution. I just wanted to see what others have.
- cyb in United Statespublic String canonicalPath(String path){ if(path == null || path.isEmpty()){ throw new RuntimeException("incorrect path provided"); } String[] chunks = path.split("/"); Stack<String> s = new Stack<String>(); List<String> arr = new ArrayList<String>(); for(String chunk: chunks){ if(chunk.isEmpty() || chunk == "."){ System.out.println("skipping"); }else{ if(!s.isEmpty() && s.peek().equals("..") && !chunk.equals("..")){ while (!s.isEmpty()) { if(s.peek().equals("..")){ s.pop(); }else{ s.pop(); break; } } s.push(chunk); }else{ s.push(chunk); } } } StringBuffer sb = new StringBuffer(); List<String> list = null; if(!s.isEmpty()){ list = new ArrayList<String>(s); } if(list != null){ for(String ss : list){ sb.append("/"+ss); } } return sb.toString(); }
| Report Duplicate | Flag | PURGE
Amazon Software Developer Data Structures
RepHondaRonda, Accountant at Achieve Internet
I am a communications specialist who develops and nurtures relationships between an organisation, members of the media and the public ...
RepTheresaHudson, IC3 at Broadsoft
I am Theresa , a Packer with a strong determination to finish all assignments in a timely manner. Inspected and ensured ...
Reptracyremly, Anesthesiologist at Ness
I am Tracy, working as an Anesthesiologist handles surgical patients and their pain relief during procedures.Rather than my job ...
Repmargaratlonger, Talent Acquisition at Huawei
Hello, I am Margaret . I have been working with the company of Heilig-Meyers for the last 7 year. I guide ...
Reptunbanataba, Court Reporter at Wildlife officer
I am a court reporter . My work is often licensed and to record proceedings using a steno type machine. through ...
RepEileenBrown, Android Engineer at ABC TECH SUPPORT
Eileen Brown and I am a Product designer.And nowadays I am doing new research like istikhara for love marriage ...
Repshushichiku, Associate at ASU
PaulKenda is a Farmworker working at Jackhammer Technologies . I am a specialist in my work here. I manage different things ...
Your solution is same as the previous solution posted. @gilles_monnier@yahoo.fr rightly pointed out what if same page is visited by multiple users. Keying the map by pageId would overwrite the map of <userId, NumberOfVisit>.
- cyb February 02, 2015The solution could be Map<PageId, List<Map<userId, visit>>>. But as suggested below I don't think it's the most optimal solution.