
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Longest Substring Without Repeating Characters in Python
In Python, by using different methods we can find the longest substring without repeating characters, such as by using the Brute force approach which involves checking all the Substrings, and also by using the advanced Sliding window Two Pointer approach, which uses a Hashmap to store the indexes.
Some Common Methods
Some of the common approaches in Python to find the longest Substring Without Repeating Characters are as follows.
-
Brute Force: Checks all possible substrings and verifies if they have all unique characters in the string.
-
Sliding Window with Set: This approach tracks characters in the current substring using a set and a sliding window mechanism.
-
Sliding Window with Dictionary: This approach also uses a sliding window mechanism along with a dictionary which stores the last occurrence of each character.
Brute Force Approach
This method is the simplest concept but highly inefficient for large strings, as it takes Nested loops where the outer loop starts from each character, and the inner loop checks all possible substrings starting from that character.
Example
In the below example code is_unique() function checks if a substring has all unique characters by comparing the length of the characters with the length of the substring.
def length_of_longest_substring(s: str) -> int: def is_unique(substring): return len(set(substring)) == len(substring) max_length = 0 for i in range(len(s)): for j in range(i + 1, len(s) + 1): if is_unique(s[i:j]): # Update the maximum length if the current substring is longer if j - i > max_length: max_length = j - i longest_substring = s[i:j] # Print the longest substring and its length print(f"Longest substring without repeating characters: '{longest_substring}'") print(f"Length of the longest substring: {max_length}") return max_length s = "abcabcbb" length_of_longest_substring(s)
Following is the output of the above program −
Longest substring without repeating characters: 'abc' Length of the longest substring: 3
Sliding Window with Set
The set in this sliding window technique tracks the characters in the current substring and, the sliding window provides right and left pointers.
The right pointer expands the window and if a duplicate character is found then the left pointer moves right to remove characters until the duplicate is removed.
Example
From the below code, the max_length stores the maximum length of the substring without repeating characters and the start_index of the longest substring found, used to extract the substring.
def length_of_longest_substring(s: str) -> int: # Set to store unique characters of the current window char_set = set() left = 0 max_length = 0 start_index = 0 # Iterate through the string using the right pointer for right in range(len(s)): # If the character at the right pointer is already in the set, move the left pointer to the right while s[right] in char_set: char_set.remove(s[left]) left += 1 char_set.add(s[right]) if right - left + 1 > max_length: max_length = right - left + 1 start_index = left longest_substring = s[start_index:start_index + max_length] print(f"Longest substring without repeating characters: '{longest_substring}'") print(f"Length of the longest substring: {max_length}") return max_length s = "abcdabcddef" length_of_longest_substring(s)
Following is the output of the above program −
Longest substring without repeating characters: 'abcd' Length of the longest substring: 4
Sliding Window with Dictionary
This method also uses a sliding window along with a dictionary, it stores the last occurrence of each character.
Here the right pointer moves right, and if the current character already exist in the dictionary, the left pointer jumps to the right of the last occurrence of this character.
Example
From the below code ,char_index_map will the last seen index of each character, the max_length stores the maximum length of the substring, and start_index is used to extract and print the longest substring without repeating characters.
def length_of_longest_substring(s: str) -> int: # Dictionary to store the last index of each character char_index_map = {} max_length = 0 left = 0 start_index = 0 for right in range(len(s)): if s[right] in char_index_map and char_index_map[s[right]] >= left: left = char_index_map[s[right]] + 1 char_index_map[s[right]] = right if right - left + 1 > max_length: max_length = right - left + 1 start_index = left longest_substring = s[start_index:start_index + max_length] print(f"Longest substring without repeating characters: '{longest_substring}'") print(f"Length of the longest substring: {max_length}") return max_length s = "abcabcbb" length_of_longest_substring(s)
Output
Longest substring without repeating characters: 'ab' Length of the longest substring: 2