Skip to content

Commit 645684b

Browse files
add a solution for 763
1 parent 6d58fe9 commit 645684b

File tree

2 files changed

+77
-21
lines changed

2 files changed

+77
-21
lines changed
Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package com.fishercoder.solutions;
22

33
import java.util.ArrayList;
4+
import java.util.HashMap;
45
import java.util.List;
6+
import java.util.Map;
57

68
public class _763 {
79

810
public static class Solution1 {
9-
public List<Integer> partitionLabels(String S) {
11+
public List<Integer> partitionLabels(String s) {
1012
List<Integer> result = new ArrayList<>();
1113
int[] last = new int[26];
1214
/**This is the key step:
1315
* we find the last occurrence of each letter and record them in last[]*/
14-
for (int i = 0; i < S.length(); i++) {
15-
last[S.charAt(i) - 'a'] = i;
16+
for (int i = 0; i < s.length(); i++) {
17+
last[s.charAt(i) - 'a'] = i;
1618
}
1719
/**record the last end index of the current substring*/
1820
int end = 0;
1921
int start = 0;
20-
for (int i = 0; i < S.length(); i++) {
21-
end = Math.max(end, last[S.charAt(i) - 'a']);
22+
for (int i = 0; i < s.length(); i++) {
23+
end = Math.max(end, last[s.charAt(i) - 'a']);
2224
if (end == i) {
2325
result.add(end - start + 1);
2426
start = end + 1;
@@ -28,4 +30,35 @@ public List partitionLabels(String S) {
2830
}
2931
}
3032

33+
public static class Solution2 {
34+
/**
35+
* My completely original solution on 10/14/2021.
36+
*
37+
* Again, using a pen and paper to visualize how this works,
38+
* from the left to the right of the given string helps
39+
* sort out the algorithm greatly and clears up any ambiguities!
40+
*/
41+
public List<Integer> partitionLabels(String s) {
42+
List<Integer> ans = new ArrayList<>();
43+
Map<Character, Integer> lastIndexMap = new HashMap<>();
44+
for (int i = 0; i < s.length(); i++) {
45+
lastIndexMap.put(s.charAt(i), i);
46+
}
47+
for (int i = 0; i < s.length(); i++) {
48+
int boundary = i;
49+
int start = i;
50+
do {
51+
int lastIndex = lastIndexMap.get(s.charAt(i));
52+
boundary = Math.max(lastIndex, boundary);
53+
i++;
54+
} while (i < boundary);
55+
if (i > boundary) {
56+
i--;
57+
}
58+
ans.add(boundary - start + 1);
59+
}
60+
return ans;
61+
}
62+
}
63+
3164
}
Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._763;
4+
45
import java.util.Arrays;
6+
import java.util.List;
7+
58
import org.junit.BeforeClass;
69
import org.junit.Test;
710

811
import static org.junit.Assert.assertEquals;
912

1013
public class _763Test {
11-
private static _763.Solution1 solution1;
12-
13-
@BeforeClass
14-
public static void setup() {
15-
solution1 = new _763.Solution1();
16-
}
17-
18-
@Test
19-
public void test1() {
20-
assertEquals(Arrays.asList(9, 7, 8), solution1.partitionLabels("ababcbacadefegdehijhklij"));
21-
}
22-
23-
@Test
24-
public void test2() {
25-
assertEquals(Arrays.asList(9, 7, 8), solution1.partitionLabels("ababcbacadefegdehijhklij"));
26-
}
14+
private static _763.Solution1 solution1;
15+
private static _763.Solution2 solution2;
16+
private static List<Integer> expected;
17+
private static String s;
18+
19+
@BeforeClass
20+
public static void setup() {
21+
solution1 = new _763.Solution1();
22+
solution2 = new _763.Solution2();
23+
}
24+
25+
@Test
26+
public void test1() {
27+
expected = Arrays.asList(9, 7, 8);
28+
s = "ababcbacadefegdehijhklij";
29+
assertEquals(expected, solution1.partitionLabels(s));
30+
assertEquals(expected, solution2.partitionLabels(s));
31+
}
32+
33+
@Test
34+
public void test2() {
35+
expected = Arrays.asList(10);
36+
s = "eccbbbbdec";
37+
assertEquals(expected, solution1.partitionLabels(s));
38+
assertEquals(expected, solution2.partitionLabels(s));
39+
}
40+
41+
@Test
42+
public void test3() {
43+
expected = Arrays.asList(1, 9);
44+
s = "caedbdedda";
45+
assertEquals(expected, solution1.partitionLabels(s));
46+
assertEquals(expected, solution2.partitionLabels(s));
47+
}
48+
49+
2750
}

0 commit comments

Comments
 (0)