. Leetcode no repeated character notes -3 longest substring - CrowFea

0.5 Description of the problem

Given a string containing the length of the longest sub not identify duplicate character strings.

Example 1:

Input:

1
"abcabcbb"

Output:

1
3

Explanation: no repeat longest substring of characters is "abc", has a length of 3.

Example 2:

Input:

1
"bbbbb"

Output:

1
1

Explanation: no repeat longest substring of characters is "b", its length is 1.

Example 3:

Input:

1
"pwwkew"

Output:

1
3

Explanation: no repeat longest substring of characters is "wke", its length is 3.
Please note that the answer must be a substring, "pwke" is a sequence rather than a string.

1. Analysis

This question of time to see the thought is to use a hash table to do, so that you can complete re-check in linear time, then watched goose answer this question or emmm.

Gave us a string, we let the child find the longest non-repeating character string, note that there is a substring, not the sequence, it must be continuous . From the perspective of the human brain, if you give an example of "abcacbacas", allows you to manually find no duplicate character in the string, how to find? A character traversing a character, such as a, b, c, and then there was a a, then the time should remove the first occurrence of a, then continue to the next, there was a c, should be removed once appeared of C, and so on, eventually found the longest length of 3.

The idea from the beginning was to record the location of all the letters, but because the letters will be repeated, do not actually meaning, but also confusion. Further consideration, since the character will be repeated, in the end is to save all occurrences, or is it only records a position?

We derive the manual method before actually maintains a sliding window are no repeating characters in the window, we need to expand the size of the window as possible. Since the window kept sliding to the right, so we only care about the position of the last occurrence of each character, and establish a mapping. The position of the right border of the window is the visiting to the character, in order to obtain the size of the window, we need a variable to point to the left of the left edge of the sliding window, so if the current traverse to the character never appeared, so direct for right border, if there before too, then two cases: in or out of the sliding window, if not sliding window, then right, the current character you can add to the mix, if so, you need to remove this in the sliding window the character has appeared, the method does not remove the need to traverse the left border left one bit to the right to find, because we have the HashMap save the location of the last occurrence of repeated characters, so a direct move left pointer on it. We maintain a result res, each with a window size appeared to update the results res, you can get the final result.

Can

Establish a 256-bit integer array size instead of the hash table , the reason for this is the ASCII table can represent a total of 256 characters, so you can record all the characters , then we need to define two variables res and left, which used to res recording a longest substring no repeat length, left no repeat point start position of the left substrings, then we traverse the entire string, for each of the traversed character, if the string corresponds to a value in the hash table 0, indicating that the character is not encountered, the longest case calculated without duplication substring, i - left +1, where i is the longest string of the rightmost position without repeats, left is the leftmost position, there is a case also no need to calculate the longest repeated substring, that is, when the value of the hash table is less than left, this is because at this time there have been repeated characters, left the position of update, and if they encounter a new character, it is necessary recalculating the longest substring without duplication. Finally, every time the current character in the hash table corresponding to a value assigned to i + 1.

3. Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class  {
public:
int lengthOfLongestSubstring(string s) {
int m[256]={0}, i,left=0,res=0;
for (i = 0; i < s.size(); i++) {
if (m[s[i]] == 0||m[s[i]]<left) {
if (res < i - left + 1) {
res = i - left + 1;
}
}
else {
left = m[s[i]];
}
m[s[i]] = i + 1;
}
return res;
}
};

Here the program explained that if the conditional statement why there must be m [s [i]] <left, we use an example to illustrate, if the input string is "abbca" time, when i = 4, but also when that is about to start traversing a last letter, the hash table at this time corresponds to a 1, b corresponding to 3, c corresponding to 4, left 2, i.e., the current longest substring to the left edge of the second b position and has a first not in the current longest string of the range, then for i = 4 of a new incoming, the results should be added, but this time is not updated as a hash table 1, not 0, if not determine the relationship between it and the left of it, will not be able to update the results, then the answer would be a little, it is necessary to add m [s [i]] <left.

4. Compact Code

1
2
3
4
5
6
7
8
9
10
11
12
13
class  {
public:
int lengthOfLongestSubstring(string s) {
vector<int> m(256, -1);
int res = 0, left = -1;
for (int i = 0; i < s.size(); ++i) {
left = max(left, m[s[i]]);
m[s[i]] = i;
res = max(res, i - left);
}
return res;
}
};

Then the final intentions of the heart deeply about the hash table to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
class  {
public:
int lengthOfLongestSubstring(string s) {
int res = 0, left = 0, i = 0, n = s.size();
unordered_map<char, int> m;
for (int i = 0; i < n; ++i) {
left = max(left, m[s[i]]);
m[s[i]] = i + 1;
res = max(res, i - left + 1);
}
return res;
}
};

5.Java Solution

C ++ uses the same idea to solve Java solution, personal feeling java method is indeed true trouble ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class  {
public int lengthOfLongestSubstring(String s) {
int n = s.length();
int maxLen = 0;
HashMap<Character, Integer> map = new HashMap<>();
for(int i = 0, j = 0; j < n; j++){
if(map.containsKey(s.charAt(j)))
i = Math.max(i, map.get(s.charAt(j)));
maxLen = Math.max(maxLen, j-i+1);
map.put(s.charAt(j),j+1);
}
return maxLen;
}
}

6.Python解法

1
2
3
4
5
6
7
8
9
10
class :
def lengthOfLongestSubstring(self, s: str) -> int:
st={}
i,ans=0,0
for j in range(len(s)):
if s[j] in st:
i=max(i,st[s[j]])
ans=max(ans,j-i+1)
st[s[j]]=j+1
return ans




原文:大专栏  leetcode笔记-3.无重复字符的最长子串 - CrowFea


Guess you like

Origin www.cnblogs.com/petewell/p/11612220.html