-
Notifications
You must be signed in to change notification settings - Fork 0
/
LengthOfLongestSubstring.java
102 lines (75 loc) · 2.74 KB
/
LengthOfLongestSubstring.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package Algorithms.SlidingWindow;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author Srinivas Vadige, [email protected]
* @since 26 Sept 2024
*/
public class LengthOfLongestSubstring {
public static void main(String[] args) {
String s = "dvdfd";
System.out.println("lengthOfLongestSubstring: " + lengthOfLongestSubstring(s));
System.out.println("lengthOfLongestSubstringNoPointers: " + lengthOfLongestSubstringNoPointers(s));
}
public static int lengthOfLongestSubstring(String s) {
int n = s.length();
int maxLength = 0;
Set<Character> charSet = new HashSet<>();
int left = 0;
for (int right = 0; right < n; right++) {
if (!charSet.contains(s.charAt(right))) {
charSet.add(s.charAt(right));
maxLength = Math.max(maxLength, right - left + 1);
} else {
while (charSet.contains(s.charAt(right))) {
charSet.remove(s.charAt(left));
left++;
}
charSet.add(s.charAt(right));
}
}
return maxLength;
}
/*
* No nedd to use two pointer approach like MinimumWindowSubString problem. Because we're focussing only one dup char
*/
public static int lengthOfLongestSubstringNoPointers(String s) {
if(s.trim().length() == 1)
return 1;
if(s.isEmpty()) return 0;
String subStr = "";
int maxL = 0;
String[] chars = s.split("");
for(int i=0; i<s.length(); i++){
int repStrIndex = subStr.indexOf(chars[i]); // as we check the index of subStr not s. It's working fine
if(repStrIndex > -1){
subStr = subStr.substring(repStrIndex+1) + chars[i]; // --- or HashSet
} else subStr += chars[i];
System.out.println(subStr);
maxL = Math.max(maxL, subStr.length());
}
return Math.max(maxL, subStr.length());
}
// won't work for Eg: dvdf -- here max sub string length is 3
public static int lengthOfLongestSubstringHashMap(String s) {
Map<String, Integer> map = new HashMap<>();
String[] chars = s.split("");
int maxL = 0;
int tempStart=0;
for(int i=0; i<s.length(); i++){
if(map.containsKey(chars[i])){
maxL = Math.max(maxL, i-tempStart);
tempStart = i;
map.clear();
map.put(chars[i], 1);
}
else
map.put(chars[i], 1);
System.out.println(maxL);
System.out.println(map);
}
return map.keySet().size();
}
}