Skip to content

Commit 0ae5426

Browse files
authored
Create 0071-simplify-path.js
Solved simplify-path in JS.
1 parent 5f4f024 commit 0ae5426

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Diff for: javascript/0071-simplify-path.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Stack
3+
* https://leetcode.com/problems/simplify-path/
4+
*
5+
* Time O(n) | Space O(n)
6+
* @param {string} path
7+
* @return {string}
8+
*/
9+
var simplifyPath = function(path) {
10+
let currunt = '';
11+
let myStack = [];
12+
path = '/' + path + '/';
13+
for(let i = 0; i < path.length; i++) {
14+
15+
console.log(myStack);
16+
if(path[i] === '/') {
17+
if(currunt == '..') {
18+
if(myStack.length) {
19+
myStack.pop();
20+
}
21+
} else if(currunt !== '' && currunt !== '.') {
22+
myStack.push(currunt);
23+
}
24+
currunt = '';
25+
} else {
26+
currunt += path[i];
27+
}
28+
}
29+
30+
myStack = myStack.join('/');
31+
myStack = '/' + myStack;
32+
return myStack;
33+
};

0 commit comments

Comments
 (0)