Skip to content

Commit 388e40c

Browse files
committed
Adjust SimplifyPath solution to pass tricky tase-case on leetcode
1 parent 0d95b63 commit 388e40c

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/main/java/by/andd3dfx/collections/SimplifyPath.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,7 @@ public static String simplifyPath(String path) {
7070
while (i < chars.length) {
7171
switch (chars[i]) {
7272
case '/':
73-
if (accumulator.toString().equals("..") && !stack.isEmpty()) {
74-
stack.pop();
75-
} else {
76-
checkAndPush(stack, accumulator);
77-
}
73+
process2DotsCase(accumulator, stack);
7874
accumulator = new StringBuilder();
7975
break;
8076

@@ -83,14 +79,22 @@ public static String simplifyPath(String path) {
8379
}
8480
i++;
8581
}
86-
checkAndPush(stack, accumulator);
82+
process2DotsCase(accumulator, stack);
8783

8884
List<String> folders = Arrays.stream(stack.toArray(new String[0]))
8985
.toList()
9086
.reversed();
9187
return "/" + String.join("/", folders);
9288
}
9389

90+
private static void process2DotsCase(StringBuilder accumulator, Deque<String> stack) {
91+
if (accumulator.toString().equals("..") && !stack.isEmpty()) {
92+
stack.pop();
93+
} else {
94+
checkAndPush(stack, accumulator);
95+
}
96+
}
97+
9498
private static void checkAndPush(Deque<String> stack, StringBuilder accumulator) {
9599
var string = accumulator.toString();
96100
if (!string.isEmpty() && !string.matches("\\.{1,2}")) {

src/test/java/by/andd3dfx/collections/SimplifyPathTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public void testSimplifyPath() {
1515
assertThat(SimplifyPath.simplifyPath("/../")).isEqualTo("/");
1616
assertThat(SimplifyPath.simplifyPath("/.../a/../b/c/../d/./"))
1717
.isEqualTo("/.../b/d");
18+
assertThat(SimplifyPath.simplifyPath("/a//b////c/d//././/..")).isEqualTo("/a/b/c");
1819
}
1920
}

0 commit comments

Comments
 (0)