Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1404 from loczek/71-Simplify-Path
Browse files Browse the repository at this point in the history
Create: 71-Simplify-Path.ts
  • Loading branch information
dissesmac authored Nov 2, 2022
2 parents 386527f + 4a0ada9 commit 21cb5b1
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions typescript/71-Simplify-Path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function simplifyPath(path: string): string {
const stack: string[] = [];
let cur = '';

for (const c of path + '/') {
if (c === '/') {
if (cur === '..') {
if (stack.length > 0) {
stack.pop();
}
} else if (cur != '' && cur != '.') {
stack.push(cur);
}
cur = '';
} else {
cur += c;
}
}

return '/' + stack.join('/');
}

0 comments on commit 21cb5b1

Please sign in to comment.