-
Notifications
You must be signed in to change notification settings - Fork 13
/
SlidingWin_LenOfLongestSubstringNoRepChars.java
69 lines (58 loc) · 1.95 KB
/
SlidingWin_LenOfLongestSubstringNoRepChars.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
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
// https://ide.geeksforgeeks.org/v6QnWgI9xH
// https://www.ideserve.co.in/learn/longest-substring-with-non-repeating-characters
// https://practice.geeksforgeeks.org/problems/length-of-the-longest-substring/0
// https://leetcode.com/problems/longest-substring-without-repeating-characters/
class SlidingWin_LenOfLongestSubstringNoRepChars {
private static int getLengthOfLongestSubstringNoRepeatingChars (String s) {
if(s == null || s.length() == 0) return 0;
Set<Character> set = new HashSet<>();
int start = 0;
int end = 0;
int maxLen = Integer.MIN_VALUE;
for(end = 0; end < s.length(); end++) {
char endChar = s.charAt(end);
while(set.contains(endChar)) {
set.remove(s.charAt(start));
++start;
}
set.add(endChar);
maxLen = Math.max(maxLen, set.size());
}
return maxLen;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0) {
String str = br.readLine().trim();
//int len = lengthOfLongestSubstring(str);
//System.out.println(len);
System.out.println(getLengthOfLongestSubstringNoRepeatingChars(str));
}
}
/*static int lengthOfLongestSubstring(String s) {
int maxLength = Integer.MIN_VALUE;
HashSet<Character> set = new HashSet<Character>();
int i = 0, j = 0;
while ( j < s.length() ) {
if(!set.contains(s.charAt(j))) {
set.add(s.charAt(j));
j++;
System.out.println("j "+j+" i "+i);
maxLength = Math.max(maxLength, j-i);
}
else {
set.remove(s.charAt(i));
System.out.println("j "+j+" i "+i);
i++;
}
System.out.println(set);
}
return maxLength;
}*/
}