Skip to content

Commit 84a2dd4

Browse files
authored
Merge pull request neetcode-gh#2406 from sumit16sharma/patch-1
Create 0071-simplify-path.java
2 parents 34d4fc4 + a11fe71 commit 84a2dd4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

java/0071-simplify-path.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public String simplifyPath(String path) {
3+
Stack<String> stack = new Stack<>();
4+
StringBuilder curr = new StringBuilder();
5+
6+
String newPath = path + "/";
7+
8+
for(int i=0;i<newPath.length();i++) {
9+
char ch = newPath.charAt(i);
10+
11+
if(ch == '/') {
12+
if(curr.toString().equals("..")) {
13+
if(!stack.isEmpty()) {
14+
stack.pop();
15+
}
16+
} else if(!curr.isEmpty() && !curr.toString().equals(".")) {
17+
stack.push(curr.toString());
18+
}
19+
20+
curr = new StringBuilder();
21+
} else {
22+
curr.append(ch);
23+
}
24+
}
25+
26+
curr = new StringBuilder();
27+
28+
while(!stack.isEmpty()) {
29+
curr.insert(0, "/" + stack.pop());
30+
}
31+
32+
if(curr.length() == 0) {
33+
curr.append('/');
34+
}
35+
36+
return curr.toString();
37+
}
38+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy