Topic
Given a string, find the length of the oldest string that does not contain repeating characters.
Example 1:
Input: "ABCABCBB"
Output: 3
Explanation: The oldest string of non-repeating characters is "abc", with a length of 3.
Example 2:
Input: "BBBBB"
Output: 1
Explanation: The oldest string of non-repeating characters is "B", and its length is 1.
Example 3:
Input: "Pwwkew"
Output: 3
Explanation: The oldest string of non-repeating characters is "Wke", which is 3 in length.
Note that the answer must be a substring, and "Pwke" is a subsequence rather than a substring.
Analysis of brute force algorithms
/* C++代码*/public class Solution { public int lengthOfLongestSubstring(string s) { int n = s.length(); int ans = 0; for (int i = 0; i < n; i++){ for (int j = i + 1; j <= n; j++){ if (allUnique(s, i, j)) { ans = std::max(ans, j - i); } } } return ans; } public bool allUnique(string s, int start, int end) { set<char> m_set; for (int i = start; i < end; i++) { if (m_set.find(s[i]) == m_set.end()){ return false; } m_set.insert(s[i]); } return true; }}
We can easily write a brute-force algorithm that checks all of the strings for duplicate letters, enumerates the time Complexity O (n^2) of the index range (subscript range) of all strings, and iterates through the strings for a complex O (n) time. The total time complexity of the algorithm is O (n^3). This complexity is not acceptable. We need a faster approach.
How slow is the brute force algorithm?
We envision a string Abcdeafaghthat uses the brute force algorithm to solve the process
It can be seen that the algorithm scans the Bcdeafa in this range several times, but in addition to the first scan, the really useful scan is only once, that is, for the first time, a substring of length 6 is determined bcdeaf. The naked eye can easily find that follow-up is not necessary to repeat the scan, should be at the time of the second scan directly starting from F scan is the best. Brute force algorithms waste a lot of time on meaningless scans. We need to focus on solving this problem.
How to Solve
We need to know where to start the next scan when matching to a repeating character. This can save a lot of time. We can record the position of this character every time we scan to a character, and when we match it to the same character, by querying the position of the character before it appears, we position the beginning of the next scan to the next position of the last occurrence of the repeating character, see
Code implementation
/* C + + code */class solution {Public:int lengthoflongestsubstring (string s) {Unordered_map<char,i Nt> map; Auto Itor = Map.end (); int max = 0, length = 1; if (S.length () <= 1) {return s.length (); } map.insert (Pair<char,char> (s[0],0)); for (int i = 1; i < s.length ();) {itor = Map.find (S[i]); If the character precedes the IF (itor! = Map.end ()) {//jumps to the next position where the character last occurred i = Itor->second + 1 ; Clears the character position record table map.clear (); Map.insert (pair<char,char> (s[i],i)); max = Std::max (max, length); length = 1; i++; } else{Map.insert (pair<char,char> (s[i],i)); length++; i++; }} max = Std::max (max, length); return Max; }};
Leetcode the oldest string with no repeating characters