Longest Distinct characters in string Python

PROGRAM TO FIND THE LENGTH OF THE LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS



def longestUniqueSubsttr(string):
       
    # Creating a set to store the last positions of occurrence
    seen = {}
    maximum_length = 0
   
    # starting the inital point of window to index 0
    start = 0 
       
    for end in range(len(string)):
   
        # Checking if we have already seen the element or not
        if string[end] in seen:
  
            # If we have seen the number, move the start pointer
            # to position after the last occurrence
            start = max(start, seen[string[end]] + 1)
   
        # Updating the last seen value of the character
        seen[string[end]] = end
        maximum_length = max(maximum_length, end-start + 1)
    return maximum_length
   
# Driver Code
string = "geeksforgeeks"
print("The input string is", string)
length = longestUniqueSubsttr(string)
print("The length of the longest non-repeating character substring is", length)

OUTPUT
The input string is geeksforgeeks
The length of the longest non-repeating character substring is 7

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java