Skip to content

Commit

Permalink
HashingTest > lengthOfLongestSubstring()
Browse files Browse the repository at this point in the history
  • Loading branch information
bky373 committed Jun 8, 2024
1 parent 4e19d1c commit 068a191
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/test/java/HashingTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;

public class HashingTest {
Expand Down Expand Up @@ -31,4 +33,49 @@ void productExceptSelf() {
// assertThat(res).isEqualTo(new int[]{0, 0, 9, 0, 0});
// assertThat(res).isEqualTo(new int[]{12, 16, 24, 48, 24});
}

@Test
void lengthOfLongestSubstring() {
// String s = "abcabcb";
String s = "bbb";
// String s = "pwwkew";
// String s = "dvdf";
// String s = " abc";
int start = 0, k = 0, longest = 0, mapVersion = 0;
Map<Character, Integer> charMap = new HashMap<>();
// b[s][k]bbb - curVersion[b]: 0, mapVersion: 0
// b[s]b[k]bb - curVersion[b]: 1, mapVersion: 0
// - [Map Update] mapVersion: 1, longest = 1
// bb[s][k]bb - curVersion[b]: 1, mapVersion: 1
// bb[s]b[k]b - curVersion[b]: 2, mapVersion: 1
// - [Map Update] mapVersion: 2, longest = 1

// a[s][k]bcabcbb - curVersion[a]: 0, mapVersion: 0
// a[s]b[k]cabcbb - curVersion[b]: 0, mapVersion: 0
// a[s]bc[k]abcbb - curVersion[c]: 0, mapVersion: 0
// a[s]bca[k]bcbb - curVersion[a]: 1, mapVersion: 0
// - [Map Update] mapVersion: 1, longest = 3
// abca[s][k]bcbb - curVersion[a]: 1, mapVersion: 1
// abca[s]b[k]cbb - curVersion[b]: 1, mapVersion: 1
// ...

while (start + k < s.length()) {
char curr = s.charAt(start + k);
// System.out.println("charMap = " + charMap);
// System.out.printf("s[%c],k[%c]\n\n", s.charAt(start), curr);
int curVersion = charMap.getOrDefault(curr, 0);
if (curVersion > mapVersion) {
longest = Math.max(longest, k);
start++;
k = 0;
mapVersion = curVersion;
} else {
charMap.put(curr, mapVersion + 1);
k++;
}
}
longest = Math.max(longest, k);

assertThat(longest).isEqualTo(3);
}
}

0 comments on commit 068a191

Please sign in to comment.