문제 Given a string, find the length of the longest substring without repeating characters.

 

예시는 https://leetcode.com/problems/longest-substring-without-repeating-characters/

 

Longest Substring Without Repeating Characters - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

링크에서 확인

 

 

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
class Solution {
    public int lengthOfLongestSubstring(String s) {
        int resCnt = 0;
        if (s.length() > 0) {
            
            char[] a = s.toCharArray();
            
            for (int i = 0; i < a.length; i++) {
                int tempCnt = 0;
                HashMap<Character, Integer> strArr = new HashMap<Character, Integer>();
                tempCnt++;
                strArr.put(a[i],i);
                for (int j = i + 1; j < a.length; j++) {
                    if (strArr.containsKey(a[j])) {
                        break;
                    } else {
                        tempCnt++;
                    }
                    strArr.put(a[j],i);
                }
                resCnt = (tempCnt > resCnt) ? tempCnt : resCnt;
            }
        }
        return resCnt;
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

시간복잡도 O(n^2)

 

1.hashmap에 담았을때가 두번째로 빠르다.

2.첫번째로 빠른것은 int형 배열

3.list방식은 가장 느리다.

 

 

 

+ Recent posts