Print Longest substring without repeating characters
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
Examples:
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].
Implementation:
C++
// C++ program to find and print longest // substring without repeating characters. #include <bits/stdc++.h> using namespace std; // Function to find and print longest // substring without repeating characters. string findLongestSubstring(string str) { int i; int n = str.length(); // starting point of current substring. int st = 0; // length of current substring. int currlen; // maximum length substring without repeating // characters. int maxlen = 0; // starting index of maximum length substring. int start; // Hash Map to store last occurrence of each // already visited character. unordered_map<char, int> pos; // Last occurrence of first character is index 0; pos[str[0]] = 0; for (i = 1; i < n; i++) { // If this character is not present in hash, // then this is first occurrence of this // character, store this in hash. if (pos.find(str[i]) == pos.end()) pos[str[i]] = i; else { // If this character is present in hash then // this character has previous occurrence, // check if that occurrence is before or after // starting point of current substring. if (pos[str[i]] >= st) { // find length of current substring and // update maxlen and start accordingly. currlen = i - st; if (maxlen < currlen) { maxlen = currlen; start = st; } // Next substring will start after the last // occurrence of current character to avoid // its repetition. st = pos[str[i]] + 1; } // Update last occurrence of // current character. pos[str[i]] = i; } } // Compare length of last substring with maxlen and // update maxlen and start accordingly. if (maxlen < i - st) { maxlen = i - st; start = st; } // The required longest substring without // repeating characters is from str[start] // to str[start+maxlen-1]. return str.substr(start, maxlen); } // Driver function int main() { string str = "GEEKSFORGEEKS"; cout << findLongestSubstring(str); return 0; } |
Python3
# Python3 program to find and print longest # substring without repeating characters. # Function to find and print longest # substring without repeating characters. def findLongestSubstring(string): n = len(string) # starting point of current substring. st = 0 # maximum length substring without # repeating characters. maxlen = 0 # starting index of maximum # length substring. start = 0 # Hash Map to store last occurrence # of each already visited character. pos = {} # Last occurrence of first # character is index 0 pos[string[0]] = 0 for i in range(1, n): # If this character is not present in hash, # then this is first occurrence of this # character, store this in hash. if string[i] not in pos: pos[string[i]] = i else: # If this character is present in hash then # this character has previous occurrence, # check if that occurrence is before or after # starting point of current substring. if pos[string[i]] >= st: # find length of current substring and # update maxlen and start accordingly. currlen = i - st if maxlen < currlen: maxlen = currlen start = st # Next substring will start after the last # occurrence of current character to avoid # its repetition. st = pos[string[i]] + 1 # Update last occurrence of # current character. pos[string[i]] = i # Compare length of last substring with maxlen # and update maxlen and start accordingly. if maxlen < i - st: maxlen = i - st start = st # The required longest substring without # repeating characters is from string[start] # to string[start+maxlen-1]. return string[start : start + maxlen] # Driver Code if __name__ == "__main__": string = "GEEKSFORGEEKS" print(findLongestSubstring(string)) # This code is contributed by Rituraj Jain |
Output:
EKSFORG
Time Complexity: O(n)
Auxiliary Space: O(n)
Recommended Posts:
- Length of the longest substring without repeating characters
- Longest repeating and non-overlapping substring
- 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
- Find the longest substring with k unique characters in a given string
- Print the longest common substring
- Find the first non-repeating character from a stream of characters
- Largest substring with same Characters
- Longest substring with count of 1s more than 0s
- Longest Common Substring | DP-29
- Longest substring of vowels
- Longest Palindromic Substring | Set 2
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 1
- Maximum length substring having all same characters after k changes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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



