3. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/
Examples:
Given "abcabcbb"
, the answer is "abc"
,which the length is 3.
Given "bbbbb"
, the answer is "b"
,with the length of 1.
Given "pwwkew"
, the answer is "wke"
,with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
#include <map>
#include<string>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(strings) {
map<char,int>map1;
map<char,int>::iterator it;
int sum = 0, max = 0,index = 0;;
for (int i = 0; i <s.length(); i++)
{
if (map1.count(s.at(i)) == 0)
{
map1[s.at(i)] = i;
sum++;
if (sum>max)
{
max = sum;
}
}
else{
sum = i - map1[s.at(i)];
index = map1[s.at(i)];
it = map1.begin();
while( it != map1.end())
{
if (it->second<= index)
{
it = map1.erase(it);
}
else{
++it;
}
}
map1[s.at(i)]=i;
}
}
return max;
}
};
Approach #1 Brute Force [Time LimitExceeded]
Java
publicclassSolution{
publicintlengthOfLongestSubstring(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= Math.max(ans, j- i);
return ans;
}
publicbooleanallUnique(String s,int start,int end) {
Set<Character> set= new HashSet<>();
for (int i=start; i< end; ++i) {
Character ch = s.charAt(start);
if (set.contains(ch)) return false;
set.add(ch);
}
return true;
}
}
ComplexityAnalysis
- Time complexity : O(n^3)O(n3).
- Space complexity : O(min(n, m))O(min(n,m)). We need O(k)O(k) space for checking a substring has no duplicate characters, where kk is the size of the Set. The size of the Set is upper bounded by the size of the string nn and the size of the charset/alphabet mm.
Approach #2 Sliding Window [Accepted]
Java
publicclassSolution{
publicintlengthOfLongestSubstring(String s) {
int n= s.length();
Set<Character> set= new HashSet<>();
int ans=0, i=0, j=0;
while (i < n && j < n){
// try to extendthe range [i, j]
if (!set.contains(s.charAt(j))){
set.add(s.charAt(j++));
ans = Math.max(ans, j- i);
}
else {
set.remove(s.charAt(i++));
}
}
return ans;
}
}
Complexity Analysis
· Time complexity : O(2n) = O(n)O(2n)=O(n). In the worst case each character willbe visited twice by ii and jj.
· Space complexity : O(min(m, n))O(min(m,n)). Same as the previous approach. Weneed O(k)O(k) space forthe sliding window, where kk is the size of the Set
. The size of the Set is upper bounded by the size of the string nn and thesize of the charset/alphabet mm.
Approach #3 Sliding Window Optimized[Accepted]
Java (Using HashMap)
public class Solution {
public intlengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>(); // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; ++j) {
if (map.containsKey(s.charAt(j))) {
i = Math.max(map.get(s.charAt(j)), i);
}
ans = Math.max(ans, j - i + 1);
map.put(s.charAt(j), j + 1);
}
return ans;
}
}
- Time complexity : O(n)O(n). Index jj will iterate nn times.
- Space complexity (HashMap) : O(min(m, n))O(min(m,n)). Same as the previous approach.
修改:省去删除keys的步骤,滑动窗口方式,重复key在窗口左边界左边的取窗口左边界,判断窗口大小与当前最大值,覆盖更新重复key;
#include <map>
#include<string>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(strings) {
map<char,int>map1;
map<char,int>::iterator it;
int sum = 0, max =0,j=0;
for (int i = 0; i <s.length(); i++)
{
if (map1.count(s.at(i)) != 0)
{
if (map1[s.at(i)]>j)
{
j = map1[s.at(i)];
}
}
if (i - j +1>sum) //注意!!!
{
sum = i - j+1;
}
map1[s.at(i)] = i+1; //注意是i+1!!!
}
return sum;
}
};