Find longest substring without repeating characters

前端 未结 30 2467
轻奢々
轻奢々 2020-12-12 18:07

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

Example:

Input:

30条回答
  •  伪装坚强ぢ
    2020-12-12 19:01

    You keep an array indicating the position at which a certain character occurred last. For convenience all characters occurred at position -1. You iterate on the string keeping a window, if a character is repeated in that window, you chop off the prefix that ends with the first occurrence of this character. Throughout, you maintain the longest length. Here's a python implementation:

    def longest_unique_substr(S):
      # This should be replaced by an array (size = alphabet size).
      last_occurrence = {} 
      longest_len_so_far = 0
      longest_pos_so_far = 0
      curr_starting_pos = 0
      curr_length = 0
    
      for k, c in enumerate(S):
        l = last_occurrence.get(c, -1)
        # If no repetition within window, no problems.
        if l < curr_starting_pos: 
            curr_length += 1
        else:
            # Check if it is the longest so far
            if curr_length > longest_len_so_far: 
                longest_pos_so_far = curr_starting_pos
                longest_len_so_far = curr_length
            # Cut the prefix that has repetition
            curr_length -= l - curr_starting_pos
            curr_starting_pos = l + 1
        # In any case, update last_occurrence
        last_occurrence[c] = k
    
      # Maybe the longest substring is a suffix
      if curr_length > longest_len_so_far:
        longest_pos_so_far = curr_starting_pos
        longest_len_so_far = curr_length
    
      return S[longest_pos_so_far:longest_pos_so_far + longest_len_so_far]
    

提交回复
热议问题