Skip to content

Commit 34d4fc4

Browse files
authored
Create 0071-simplify-path.kt
1 parent 050c9c5 commit 34d4fc4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

kotlin/0071-simplify-path.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
fun simplifyPath(path: String): String {
3+
val stack = LinkedList<String>()
4+
val invalids = hashSetOf("", ".", "..")
5+
6+
path.split("/").forEach{
7+
if(it == ".." && stack.isNotEmpty()) {
8+
stack.removeLast()
9+
} else if (it !in invalids) {
10+
stack.addLast(it)
11+
}
12+
}
13+
14+
return if(stack.isEmpty()) "/" else StringBuilder().apply{
15+
while (stack.isNotEmpty()) {
16+
this.append("/")
17+
this.append(stack.removeFirst())
18+
}
19+
}.toString()
20+
}
21+
}

0 commit comments

Comments
 (0)