Leetcode Longest Substring Without Repeating Characters problem solution in Python programming

In the Leetcode Longest Substring Without Repeating Characters problem solution in Python programming Given a string s, find the length of the longest substring without repeating characters.

Leetcode Longest Substring Without Repeating Characters problem solution in Python programming

class Solution:
    # @return an integer
    def lengthOfLongestSubstring(self, s):
        result = 0
        i = 0
        ptr = 0
        flag = True
        while i < len(s):
            if flag:
                while ptr < len(s):
                    if s[ptr] not in s[i:ptr]:
                        ptr += 1
                    else:
                        result = max(result, ptr-i)
                        flag = False
                        break
            if ptr == len(s):
                break
            if s[i] == s[ptr]:
                flag = True
            i += 1
        return max(result, ptr-i)

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

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