Longest Substring Without Repeating Characters in Python


Suppose we have a string. We have to find the longest substring without repeating the characters. So if the string is like “ABCABCBB”, then the result will be 3, as there is a substring that is repeating, of length 3. That is “ABC”.

To solve this, we will follow these steps

  • set i := 0, j := 0, set one map to store information
  • ans := 0
  • while j < length of string s
    • if s[j] is not present in map, or i > map[s[j]], then
      • ans := max(ans, j – i + 1)
      • map[s[j]] := j
    • otherwise
      • i := map[s[j]] + 1
      • ans := max(ans, j – i + 1)
      • decrease j by 1
    • increase j by 1
  • return ans

Example (Python)

Let us see the following implementation to get better understanding

 Live Demo

class Solution(object):
   def lengthOfLongestSubstring(self, s):
      i =0
      j = 0
      d={}
      ans = 0
      while j < len(s):
         if s[j] not in d or i>d[s[j]]:
            ans = max(ans,(j-i+1))
            d[s[j]] = j
         else:
            i = d[s[j]]+1
            ans = max(ans,(j-i+1))
            j-=1
         #print(ans)
         j+=1
      return ans
ob1 = Solution()
print(ob1.lengthOfLongestSubstring("ABCABCBB"))

Input

"ABCABCBB"

Output

3

Updated on: 27-Apr-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements