File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments