Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df650d6

Browse files
authored
Create 71-simplify-path.js
Solved simplify-path in JS.
1 parent 3cfb9a5 commit df650d6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

javascript/71-simplify-path.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// problem link https://leetcode.com/problems/simplify-path
2+
// time complexity O(n)
3+
4+
var simplifyPath = function(path) {
5+
let currunt = '';
6+
let myStack = [];
7+
path = '/' + path + '/';
8+
for(let i = 0; i < path.length; i++) {
9+
10+
if(path[i] === '/') {
11+
if(currunt == '..') {
12+
if(myStack.length) {
13+
myStack.pop();
14+
}
15+
} else if(currunt !== '' && currunt !== '.') {
16+
myStack.push(currunt);
17+
}
18+
currunt = '';
19+
} else {
20+
currunt += path[i];
21+
}
22+
}
23+
24+
myStack = myStack.join('/');
25+
myStack = '/' + myStack;
26+
return myStack;
27+
};

0 commit comments

Comments
 (0)