Skip to content

Commit 1961676

Browse files
committed
Completed '3. Longest Substring With No Repeating Characters'
1 parent b4506a2 commit 1961676

File tree

5 files changed

+163
-18
lines changed

5 files changed

+163
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ TypeScript Solutions – [Ryo112358/leetcode-typescript](https://github.com/Ryo1
2323

2424
### Medium
2525
- <span title="Runtime Percentile: 67.95%">2. Add Two Numbers</span>
26+
- <span title="Runtime Percentile: 91.22%">3. Longest Substring Without Repeating Characters</span>
2627
- <span title="Runtime Percentile: 99.95%">6. ZigZag Conversion</span>
2728
- <span title="Runtime Percentile: 98.48%">11. Container With Most Water</span>
2829
- <span title="Runtime Percentile: 81.17%">12. Integer To Roman</span>

src/main/java/io/coffeelessprogrammer/leetcode/hard/TrappingRainWater.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,43 @@ public static int calcVolume(int[] walls) {
4747
return totalTrappedWater;
4848
}
4949

50-
/** In progress */
50+
// #region BruteForce
51+
5152
public static int calcVolumeBF(int[] walls) {
5253
int totalTrappedWater = 0;
5354

54-
List<Integer> waterTrappedPerLevel;
55+
for(int i=0; i < walls.length; ++i) {
56+
int maxLeft = findHeightTallestLeftWall(walls, i);
57+
int maxRight = findHeightTallestRightWall(walls, i);
5558

56-
for(int i=1; i < walls.length; ++i) {
57-
if(walls[i] > 0) {
58-
waterTrappedPerLevel = new ArrayList<>();
59+
int currentWater = Math.min(maxLeft, maxRight) - walls[i];
5960

60-
for(int j=i+1; j < walls.length && walls[j] < walls[i]; ++j) {
61+
if(currentWater > 0)
62+
totalTrappedWater += currentWater;
63+
}
6164

62-
System.out.println(waterTrappedPerLevel.size());
63-
for(int k=1; k < walls[i]; ++k) {
64-
waterTrappedPerLevel.add(walls[i] - walls[j]);
65-
}
66-
}
65+
return totalTrappedWater;
66+
}
6767

68-
System.out.println(Arrays.toString(waterTrappedPerLevel.toArray()));
68+
private static int findHeightTallestLeftWall(int[] walls, int startingIndex) {
69+
int tallestWall = 0;
6970

70-
for(int volumeAtDepth: waterTrappedPerLevel) {
71-
totalTrappedWater += volumeAtDepth;
72-
}
73-
}
71+
for(int i=startingIndex; i > -1; --i) {
72+
tallestWall = Math.max(walls[i], tallestWall);
7473
}
7574

76-
return totalTrappedWater;
75+
return tallestWall;
7776
}
77+
78+
private static int findHeightTallestRightWall(int[] walls, int startingIndex) {
79+
int tallestWall = 0;
80+
81+
for(int i=startingIndex; i < walls.length; ++i) {
82+
tallestWall = Math.max(walls[i], tallestWall);
83+
}
84+
85+
return tallestWall;
86+
}
87+
88+
// #endRegion
7889
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package io.coffeelessprogrammer.leetcode.medium;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
/*
9+
* Problem: 3. Longest Substring Without Repeating Characters
10+
* Acceptance Rate: 32.2%
11+
* URL: https://leetcode.com/problems/longest-substring-without-repeating-characters/
12+
*
13+
* Runtime: 83 ms, faster than 14.91% of Java online submissions for Longest Substring Without Repeating Characters.
14+
* Memory Usage: 39.6 MB, less than 47.86% of Java online submissions for Longest Substring Without Repeating Characters.
15+
*
16+
* ↓ Post Optimization
17+
*
18+
* 4 ms, faster than 91.22% of Java online submissions for Longest Substring Without Repeating Characters.
19+
* Memory Usage: 38.6 MB, less than 98.29% of Java online submissions for Longest Substring Without Repeating Characters.
20+
*/
21+
22+
public class LongestSubstringNoRepeatingChar {
23+
24+
public static int getLength(String str) {
25+
Map<Character, Integer> found = new HashMap<>();
26+
27+
int lengthOfLongestSubstring = 0, windowStart=0;
28+
29+
for(int i=0; i < str.length(); ++i) {
30+
// System.out.println("substringStart=" + windowStart + ", i=" + i + ", char=" + str.charAt(i));
31+
32+
if(found.containsKey(str.charAt(i))) {
33+
int repeatedCharIndex = found.get(str.charAt(i));
34+
if(repeatedCharIndex >= windowStart) {
35+
windowStart = repeatedCharIndex + 1;
36+
}
37+
}
38+
found.put(str.charAt(i), i);
39+
lengthOfLongestSubstring = Math.max(lengthOfLongestSubstring, i-windowStart+1);
40+
}
41+
42+
// System.out.println("Result for '" + str + "' = " + lengthOfLongestSubstring + "\n\n");
43+
44+
return lengthOfLongestSubstring;
45+
}
46+
47+
// #region BruteForce
48+
49+
public static int getLengthBF(String str) {
50+
Map<Character, Integer> found = new HashMap<>();
51+
52+
int i=0, j=0;
53+
54+
int lengthOfLongestSubstring = 0;
55+
56+
while(i < str.length()) {
57+
System.out.println("substringStart=" + j + ", i=" + i + ", char=" + str.charAt(i));
58+
59+
if(found.containsKey(str.charAt(i))) {
60+
System.out.println(" Repeat found X_X");
61+
if(i-j > lengthOfLongestSubstring) lengthOfLongestSubstring = i-j;
62+
63+
j = found.get(str.charAt(i)) + 1;
64+
i = j-1;
65+
66+
found.clear();
67+
} else {
68+
System.out.println(" Adding :)");
69+
found.put(str.charAt(i), i);
70+
}
71+
72+
++i;
73+
74+
if(i == str.length()) {
75+
if(i-j > lengthOfLongestSubstring) lengthOfLongestSubstring = i-j;
76+
}
77+
}
78+
79+
System.out.println("Result for '" + str + "' = " + lengthOfLongestSubstring + "\n\n");
80+
81+
return lengthOfLongestSubstring;
82+
}
83+
84+
// #endRegion
85+
86+
public static int jeantimax(String s) {
87+
int i = 0, j = 0, max = 0;
88+
Set<Character> set = new HashSet<>();
89+
90+
while (j < s.length()) {
91+
if (!set.contains(s.charAt(j))) {
92+
set.add(s.charAt(j++));
93+
max = Math.max(max, set.size());
94+
} else {
95+
set.remove(s.charAt(i++));
96+
}
97+
}
98+
99+
return max;
100+
}
101+
}

src/main/java/io/coffeelessprogrammer/leetcode/medium/ZigZagConversion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Runtime: 8 ms, faster than 54.57% of Java online submissions for ZigZag Conversion.
1212
* Memory Usage: 39.4 MB, less than 77.06% of Java online submissions for ZigZag Conversion.
1313
*
14-
* ↓ After Optimization
14+
* ↓ Post Optimization
1515
*
1616
* Runtime: 2 ms, faster than 99.95% of Java online submissions for ZigZag Conversion.
1717
* Memory Usage: 38.9 MB, less than 97.86% of Java online submissions for ZigZag Conversion.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.coffeelessprogrammer.leetcode.medium;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.Assertions;
5+
6+
public class LongestSubstringNoRepeatingCharTests {
7+
8+
@Test
9+
final void HappyPath() {
10+
Assertions.assertEquals(3, LongestSubstringNoRepeatingChar.getLength("abccabb"));
11+
}
12+
13+
@Test
14+
final void Overlap() {
15+
Assertions.assertEquals(4, LongestSubstringNoRepeatingChar.getLength("abcbda"));
16+
}
17+
18+
@Test
19+
final void EmptyString() {
20+
Assertions.assertEquals(0, LongestSubstringNoRepeatingChar.getLength(""));
21+
}
22+
23+
@Test
24+
final void OneRepeatedChar() {
25+
Assertions.assertEquals(1, LongestSubstringNoRepeatingChar.getLength("ccccccc"));
26+
}
27+
28+
@Test
29+
final void TrickyOverlap() {
30+
Assertions.assertEquals(5, LongestSubstringNoRepeatingChar.getLength("anviaj"));
31+
}
32+
}

0 commit comments

Comments
 (0)