Skip to content

Commit d4d903d

Browse files
authored
Create Solution.java
1 parent ce3bd1d commit d4d903d

File tree

1 file changed

+42
-0
lines changed
  • leetcode/MEDIUM/Longest Substring Without Repeating Characters

1 file changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Brute Force
2+
3+
class Solution {
4+
public int lengthOfLongestSubstring(String s) {
5+
int len = s.length();
6+
if(s == ""){
7+
return 0;
8+
}
9+
10+
int max_count = 0;
11+
int count = 0;
12+
13+
Map map = new HashMap<Character,Integer>();
14+
15+
int i = 0;
16+
int start = 0;
17+
int tmp = 0;
18+
while(i<len){
19+
//System.out.println(map);
20+
if(map.containsKey(s.charAt(i))){
21+
if(count > max_count){
22+
max_count = count;
23+
}
24+
i = start+1;
25+
start++;
26+
count = 0;
27+
map.clear();
28+
}
29+
else{
30+
map.put(s.charAt(i),tmp++);
31+
count++;
32+
i++;
33+
}
34+
}
35+
36+
if(count > max_count){
37+
max_count = count;
38+
}
39+
40+
return max_count;
41+
}
42+
}

0 commit comments

Comments
 (0)