본문 바로가기

hacking or software engineering skills/leetcode

3. Longest Substring Without Repeating Characters

728x90

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:
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        # abcab
        # seroksrthor
        # bbiacdkbtuicdfegrfeg
        # abcdefghijklmnopqwerstuvwabxyzcdefgeh
        # 첫번째 문자를 선두로 substring 찾기.
        str_length = len(s)
        if str_length == 0:
            return 0
        elif str_length == 1:
            return 1
        longest_length = 1
        
        for start in range(str_length):
            end = start+1
            temp_length = 1    
            if (longest_length>=str_length-start):
                return longest_length
            
            while end                if not s[end] in s[start:end]:
                    temp_length += 1
                    end += 1
                else:
                    break
            if temp_length > longest_length:
                longest_length = temp_length
                

'hacking or software engineering skills > leetcode' 카테고리의 다른 글

6. ZigZag Conversion  (0) 2019.05.09
5. Longest Palindromic Substring  (0) 2019.05.09
4. Median of Two Sorted Arrays  (0) 2019.05.09
2. Add Two Numbers  (0) 2019.05.09
1. two Sum  (0) 2019.05.09