Python function find_longest_substring(s)

Write a Python function find_longest_substring(s) that takes a string s and returns the longest substring without repeating characters. If there are multiple substrings of the same maximum length, return the first one.
Example:
python
Example input:
s = abrkaabcdefghijjxxx
Expected output:
longest_substring = abcdefghij
Constraints:

  1. The input string s will only contain lowercase English letters.
  2. The function should run efficiently even for long strings (up to 10,000 characters).
    Test Cases:
    python
    Test Case 1:
    assert find_longest_substring(“abcabcbb”) == abc
    Test Case 2:
    assert find_longest_substring(“bbbbb”) == b
    Test Case 3:
    assert find_longest_substring(“pwwkew”) == wke
    Test Case 4:
    assert find_longest_substring(“abcddefg”) == abcdefg
    Test Case 5:
    assert find_longest_substring(“anviaj”) == nviaj
    Write the find_longest_substring function that passes all the above test cases.
find the cost of your paper

Sample Answer

To solve the problem of finding the longest substring without repeating characters, we can use the sliding window technique. This approach allows us to efficiently traverse the string while maintaining a record of the characters we’ve seen and their positions. Below is the implementation of the find_longest_substring function, along with the specified test cases.

def find_longest_substring(s):
char_index = {}
start = 0
longest_start = 0
longest_length = 0

for i, char in enumerate(s):
if char in char_index and char_index[char] >= start:
start = char_index[char] + 1 # Move the start pointer to the right of the last occurrence

char_index[char] = i # Update the last seen index of the character

if (i – start + 1) > longest_length: # Update longest substring if needed
longest_length = i – start + 1
longest_start = start

return s[longest_start:longest_start + longest_length]

# Test Cases
assert find_longest_substring(“abcabcbb”) == “abc”
assert find_longest_substring(“bbbbb”) == “b”
assert find_longest_substring(“pwwkew”) == “wke”
assert find_longest_substring(“abcddefg”) == “abcdefg”
assert find_longest_substring(“anviaj”) == “nviaj”

print(“All test cases passed!”)

Explanation of the Code:

1. Initialization:

– char_index: A dictionary to store the last seen index of each character.
– start: The starting index of the current substring being examined.
– longest_start: The starting index of the longest substring found.
– longest_length: The length of the longest substring found.

2. Loop Through Characters:

– We iterate over each character in the string using enumerate to get both the index (i) and the character (char).
– If char has been seen before and its last seen index is within the current substring (start), we update start to be one position after the last occurrence of char.
– Update the last seen index of char in char_index.

3. Check for Longest Substring:

– Calculate the current length of the substring as i – start + 1. If this length is greater than longest_length, we update both longest_length and longest_start.

4. Return Result:

– Finally, we return the longest substring using string slicing.

This approach runs in O(n) time complexity, making it efficient even for long strings, as required by the constraints.

 

 

This question has been answered.

Get Answer