-
Notifications
You must be signed in to change notification settings - Fork 1
/
14thJune(I).java
55 lines (49 loc) · 1.5 KB
/
14thJune(I).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
public class LongeestSubstring {
static int lengthOfLongestSubstring1(String s) {
int n = s.length();
int res = 0;
for(int i = 0;i<n;i++){
for(int j = i; j<n; j++){
if(check(s, i , j)){
res= Math.max(res, j-i+1);
}
}
}
return res;
}
static boolean check(String s , int start , int end){ // to check if charachter is repeating
int chars[] = new int[128]; // working with alphabets ..ASCII must less than 128 for alphabets
for(int i = start; i<=end;i++){
char c = s.charAt(i);
chars[c]++;
if(chars[c]>1){
return false;
}
}
return true;
}
// Approach from the sliding window -----------------
static int lengthOfLongestSubstring(String s) {
int chars[] = new int[128];
int left = 0;
int right = 0;
int res = 0;
while(right<s.length()){
char r = s.charAt(right);
chars[r]++;
while(chars[r]>1){
char x = s.charAt(left);
chars[x]--;
left++;
}
res = Math.max(res, right-left+1);
right++;
}
return res;
}
public static void main(String[] args) {
String s = "abcabcbb";
// System.out.println(lengthOfLongestSubstring1(s));
System.out.println(lengthOfLongestSubstring(s));
}
}