-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function removeStars(s: string): string { | ||
let stack: string[] = []; | ||
|
||
for (let char of s) { | ||
if (char !== '*') { | ||
stack.push(char); | ||
} else { | ||
stack.pop(); | ||
} | ||
} | ||
|
||
return stack.join(''); | ||
} | ||
|
||
console.log(removeStars('leet**cod*e')); | ||
console.log(removeStars('erase*****')); | ||
|
||
// TODO: add other approach |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Solution 1 | ||
function simplifyPath(path: string): string { | ||
let stack: string[] = []; | ||
let ans: string = ''; | ||
let n = path.length; | ||
|
||
for (let i = 0; i < n; i++) { | ||
let dir = ''; | ||
|
||
while (i < n && path[i] !== '/') { | ||
dir += path[i]; | ||
i++; | ||
} | ||
|
||
if (dir === '..') { | ||
stack.pop(); | ||
} else if (dir === '.' || dir === '') { | ||
continue; | ||
} else { | ||
stack.push(dir); | ||
} | ||
} | ||
|
||
for (let dir of stack) { | ||
ans += '/' + dir; | ||
} | ||
|
||
return ans || '/'; | ||
} | ||
|
||
// Solution 2 | ||
function simplifyPath2(path: string): string { | ||
const stack: string[] = []; | ||
const dirs = path.split('/'); | ||
|
||
for (const dir of dirs) { | ||
if (dir === '' || dir === '.') { | ||
continue; | ||
} else if (dir === '..') { | ||
stack.pop(); | ||
} else { | ||
stack.push(dir); | ||
} | ||
} | ||
|
||
return '/' + stack.join('/'); | ||
} | ||
|
||
// Solution 3 | ||
function simplifyPath3(path: string): string { | ||
const stack: string[] = []; | ||
|
||
for (const dir of path.split('/')) { | ||
if (dir === '..') stack.pop(); | ||
else if (dir && dir !== '.') stack.push(dir); | ||
} | ||
|
||
return '/' + stack.join('/'); | ||
} |