조회 수 113 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

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

Example 1:

Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", with the length of 3. 

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3. 
             Note that the answer must be a substring, "pwke" is a subsequence and not a substring.


class Solution {
    public int lengthOfLongestSubstring(String s) {
        int maxLen = 0;
        int loop = s.length();
        int start = 0;
        int curr = 0;
        Set<Character> set = new HashSet<>();
        //HashMap<String, Sring> hashMap = new HashMap<String, Sring>();
        
        //qrsvbspk
        while(curr < loop){
            if(!set.contains(s.charAt(curr))){
                set.add(s.charAt(curr++));
                maxLen = Math.max(maxLen, set.size());
            }else{
                set.remove(s.charAt(start++));
            }
        }
        
        return maxLen;
    }
}


class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        maxlen = 0
        loop = len(s)
        start = 0
        curr = 0
        hset = set()
        
        while (curr < loop):
            if not s[curr] in hset:
                hset.add(s[curr])
                curr += 1
                maxlen = maxlen if maxlen > len(hset) else len(hset)
            else:
                hset.remove(s[start])
                start += 1
                
        return maxlen


[문제] https://leetcode.com/problems/longest-substring-without-repeating-characters/



?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
74 Leetcode 692. Top K Frequent Words hooni 2020.04.15 93
73 Leetcode 2. Add Two Numbers hooni 2020.04.14 96
72 Leetcode 731. My Calendar II hooni 2020.04.15 100
71 Leetcode 75. Sort Colors hooni 2020.04.14 101
70 Leetcode 347. Top K Frequent Elements hooni 2020.04.14 102
69 Leetcode 20. Valid Parentheses hooni 2020.04.25 102
68 Leetcode 430. Flatten a Multilevel Doubly Linked List file hooni 2020.04.06 104
67 Leetcode 23. Merge k Sorted Lists hooni 2020.04.14 104
66 Leetcode 729. My Calendar I hooni 2020.04.15 104
65 Leetcode 253. Meeting Rooms II hooni 2020.04.15 104
64 Leetcode 852. Peak Index in a Mountain Array hooni 2020.04.28 104
63 Leetcode 7. Reverse Integer hooni 2020.04.28 104
62 Leetcode 62. Unique Paths file hooni 2020.04.09 105
61 Leetcode 350. Intersection of Two Arrays II hooni 2020.04.28 106
60 Leetcode 946. Validate Stack Sequences hooni 2020.04.08 107
59 Leetcode 973. K Closest Points to Origin hooni 2020.04.15 107
58 Leetcode 443. String Compression hooni 2020.04.25 107
57 Leetcode 38. Count and Say hooni 2020.04.25 107
56 Leetcode 53. Maximum Subarray hooni 2020.04.28 110
55 Leetcode 937. Reorder Data in Log Files hooni 2020.04.25 111
Board Pagination Prev 1 2 3 4 Next
/ 4