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 a hash with key as character and value as its last position). The variable st stores the starting point of the current substring, maxlen stores the length of maximum length substring, and start stores the starting index of maximum length substring. While traversing the string, check whether the current character is present in the hash table or not. If it is not present, then store the current character in the hash table with value as the current index. If it is already present in the hash table, this means the current character could repeat in the current substring. For this check, if the previous occurrence of character is before or after the starting point st of the current substring. If it is before st, then only update the value in the hash table. If it is after st, then find the length of current substring currlen as i-st, where i is the 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 the string, the required the longest substring without repeating characters is from s[start] to s[start+maxlen-1].
Time Complexity: O(n)
Auxiliary Space: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Length of the longest substring without repeating characters
- Strings formed from given characters without any consecutive repeating characters
- Longest repeating and non-overlapping substring
- Longest substring with atmost K characters from the given set of characters
- Maximum non-repeating characters after removing K characters
- Find the longest substring with k unique characters in a given string
- Longest substring of only 4's from the first N characters of the infinite string
- Longest substring such that no three consecutive characters are same
- Length of longest substring having all characters as K
- Longest substring with K unique characters using Binary Search
- Find length of longest substring with at most K normal characters
- Longest substring whose characters can be rearranged to form a Palindrome
- Longest substring between any pair of occurrences ōf similar characters
- Print the longest common substring
- Find the first non-repeating character from a stream of characters
- Count of all unique substrings with non-repeating characters
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a 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 email@example.com. 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.