Skip to content

Commit b7d969d

Browse files
committed
LCS without Repetation
1 parent 48c17bd commit b7d969d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @author : Piyush Kumar
3+
* Problem Link : https://leetcode.com/problems/longest-substring-without-repeating-characters/
4+
*/
5+
6+
class Solution {
7+
public int lengthOfLongestSubstring(String s) {
8+
char[] ch = s.toCharArray();
9+
Map<Character, Integer> map = new HashMap<>();
10+
int max = ch.length==0 ? 0 : 1;
11+
int count = 1;
12+
for (int i=0, j=0; i < ch.length - 1; ) {
13+
map.put(ch[i], 1);
14+
if (max == ch.length) {
15+
break;
16+
}
17+
if (ch[j] != ch[j+1] && !map.containsKey(ch[j+1])) {
18+
map.put(ch[j+1], 1);
19+
j++;
20+
count++;
21+
if (count > max) {
22+
max = count;
23+
}
24+
if (j == (ch.length - 1)) {
25+
i++;
26+
j=i;
27+
count=1;
28+
}
29+
}else {
30+
map.clear();
31+
i++;
32+
j = i;
33+
count = 1;
34+
}
35+
}
36+
return max;
37+
}
38+
}
39+

0 commit comments

Comments
 (0)