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

1 0
Read Time:41 Second

Example 1:

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


Example 2:

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


Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.

Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.


Python Solution

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        if(len(s)==0):
            return 0
        listSize=[]
        temp=""
        for i in range(len(s)):
            temp=""
            for j in range(i,len(s)):
                #if element present in string break
                if s[j] in temp:
                    break
                else:
                    temp=temp+s[j]
            listSize.append(len(temp))
        if len(listSize)>=1:
            return max(listSize)
        return len(s)
        
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
100 %

About Author

Average Rating

5 Star
0%
4 Star
40%
3 Star
0%
2 Star
60%
1 Star
0%

176 thoughts on “Given a string S, find the length of the longest substring without repeating characters.

  1. Today, I went to the beach with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

Leave a Reply

Your email address will not be published. Required fields are marked *