Skip to content

Commit 21cb5b1

Browse files
authored
Merge pull request neetcode-gh#1404 from loczek/71-Simplify-Path
Create: 71-Simplify-Path.ts
2 parents 386527f + 4a0ada9 commit 21cb5b1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

typescript/71-Simplify-Path.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function simplifyPath(path: string): string {
2+
const stack: string[] = [];
3+
let cur = '';
4+
5+
for (const c of path + '/') {
6+
if (c === '/') {
7+
if (cur === '..') {
8+
if (stack.length > 0) {
9+
stack.pop();
10+
}
11+
} else if (cur != '' && cur != '.') {
12+
stack.push(cur);
13+
}
14+
cur = '';
15+
} else {
16+
cur += c;
17+
}
18+
}
19+
20+
return '/' + stack.join('/');
21+
}

0 commit comments

Comments
 (0)