Skip to content

Commit df69057

Browse files
donghyeon95donghyeon95
authored andcommitted
feat: Longest Substring Without Repeating Characters #243
1 parent fd4d875 commit df69057

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.HashMap;
2+
3+
class Solution {
4+
public int lengthOfLongestSubstring(String s) {
5+
// subString을 찾아야 한다.
6+
// for 문을 반복하면서 내가 가지고 있는 문자열에 있는 문자라면
7+
// 그 문자열까지의 길이를 기록하고 꼬리를 짜르고 다시 반복문
8+
int result = 0;
9+
HashMap<String, Boolean> hm = new HashMap<>();
10+
StringBuilder nowString = new StringBuilder();
11+
12+
for (String charater: s.split("")) {
13+
System.out.println(charater);
14+
if (hm.containsKey(charater)) {
15+
// nowString
16+
int index = nowString.indexOf(charater);
17+
nowString = new StringBuilder(nowString.substring(index+1) + charater);
18+
result = Math.max(nowString.length(), result);
19+
String removedString = nowString.substring(0, index);
20+
// String의 길이는 최대 27 * n
21+
for (String c: removedString.split("")) {
22+
hm.remove(c);
23+
}
24+
}
25+
else {
26+
hm.put(charater, true);
27+
nowString.append(charater);
28+
}
29+
}
30+
31+
32+
return Math.max(nowString.length(), result);
33+
}
34+
}

0 commit comments

Comments
 (0)