Given a string, you find out which does not contain a sub-length of the longest string of repeated characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: Because the longest substring of characters without repetition is "abc", so its length is 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: Because the longest substring is repeated characters without "b", so that its length is 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: Because the longest sub-string is repeated characters without "wke", so its length is 3.
Please note that your answer must be a substring of length, "pwke" is a sub-sequence, not a substring.
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/longest-substring-without-repeating-characters
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Mind: if S [j] with j have repeating characters' in the [I, j) range, we do not need increasing i. We can skip [i, j '] within the range of all the elements, and i becomes j' + 1.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int size,i=0,j,k,max=0;//i=begin j=end
size = s.size();
for(j = 0;j<size;j++){
for(k = i;k<j;k++)
if(s[k]==s[j]){//这里用hashmap可以更快找到与s[j]重复的位置i;
i = k+1;
break;
}
if(j-i+1 > max)
max = j-i+1;
}
return max;
}
};