Given a string, print the longest substring without repeating characters. For example, the longest substrings without repeating characters for “ABDEFGABEF” are “BDEFGA” and “DEFGAB”, with length 6. For “BBBB” the longest substring is “B”, with length 1. The desired time complexity is O(n) where n is the length of the string.
Prerequisite: Length of longest substring without repeating characters
Input : GEEKSFORGEEKS Output : EKSFORG Input : ABDEFGABEF Output : BDEFGA
Approach: The idea is to traverse the string and for each already visited character store its last occurrence in a hash table(Here unordered_map is used as hash with key as character and value as its last position). The variable st stores starting point of current substring, maxlen stores length of maximum length substring and start stores starting index of maximum length substring. While traversing the string, check whether current character is present in hash table or not. If it is not present, then store current character in hash table with value as current index. If it is already present in hash table, this means the current character could repeat in current substring. For this check if the previous occurrence of character is before or after the starting point st of current substring. If it is before st, then only update the value in hash table. If it is after st, then find length of current substring currlen as i-st, where i is current index. Compare currlen with maxlen. If maxlen is less than currlen, then update maxlen as currlen and start as st. After complete traversal of string, the required longest substring without repeating characters is from s[start] to s[start+maxlen-1].
Time Complexity: O(n)
Auxiliary Space: O(n)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Length of the longest substring without repeating characters
- Longest repeating and non-overlapping substring
- Longest substring with atmost K characters from the given set of characters
- Longest substring such that no three consecutive characters are same
- Longest substring of only 4's from the first N characters of the infinite string
- Longest substring with K unique characters using Binary Search
- Find length of longest substring with at most K normal characters
- Find the longest substring with k unique characters in a given string
- Print the longest common substring
- Strings formed from given characters without any consecutive repeating characters
- Count of all unique substrings with non-repeating characters
- Find the first non-repeating character from a stream of characters
- Print all the non-repeating words from the two given sentences
- Largest substring with same Characters
- Longest substring of vowels
- Longest Common Substring | DP-29
- Longest substring with count of 1s more than 0s
- Longest Palindromic Substring | Set 1
- Longest Palindromic Substring | Set 2
- Longest Non-palindromic substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rituraj_jain