#3 Longest Substring Without Repeating Characters[M]
Description
tags: Linked list, Math
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: 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.
Solution
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int l = 0, maxLen = 0;
map<char, vector<int>> dict;
for (int r=0; r<s.size(); r++) {
if (dict.find(s[r]) != dict.end()) {
l = max(l, dict[s[r]].back() + 1);
}
dict[s[r]].push_back(r);
maxLen = max(maxLen, r - l + 1);
}
return maxLen;
}
};
Analysis
Time:
Space:
相關文章
- 3. Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- 149 Longest Substring Without Repeating Characters
- Leetcode 3. Longest Substring Without Repeating CharactersLeetCode
- LeetCode OJ : 3 Longest Substring Without Repeating CharactersLeetCode
- Leetcode Longest Substring Without Repeating CharactersLeetCode
- Leetcode javascript 3 longest-substring-without-repeating-charactersLeetCodeJavaScript
- Leetcode-Longest Substring Without Repeating CharactersLeetCode
- Longest Substring Without Repeating Characters leetcode javaLeetCodeJava
- [LeetCode] 3. Longest Substring Without Repeating Characters 題解LeetCode
- 【LeetCode從零單排】No 3 Longest Substring Without Repeating CharactersLeetCode
- LeetCode Longest Substring Without Repeating Characters(003)解法總結LeetCode
- LeetCode3:Longest Substring Without Repeating Characters(無重複字元的最長子串)LeetCode字元
- Leet Code 3. Longest Substring Without Repeating Characters (最長的沒有重複字元的子字串)字元字串
- [LeetCode] Longest Substring Without Repeating Characters 最長無重複字元的子串LeetCode字元
- LeetCode-Longest Substring with At Least K Repeating CharactersLeetCodeAST
- LeetCode-Longest Substring with At Most K Distinct CharactersLeetCode
- Leetcode-Longest Substring with At Most Two Distinct Characters.LeetCode
- 【Leetcode】3. Longest Substring Without RepeatingCharacters無重最長子串LeetCodeGC
- Leetcode Longest Palindromic SubstringLeetCode
- LeetCode 5 (Longest Palindromic Substring)LeetCode
- LintCode-Longest Common Substring
- Leetcode-Longest Palindromic SubstringLeetCode
- Longest Palindromic Substring leetcode javaLeetCodeJava
- LeetCode 5. Longest Palindromic SubstringLeetCode
- LeetCode OJ : 5 Longest Palindromic SubstringLeetCode
- LeetCode每日一題:longest palindromic substringLeetCode每日一題
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- [LeetCode] Longest Palindromic Substring 最長迴文子串LeetCode
- Leetcode5: Longest Palindromic Substring(最長迴文子串)LeetCode
- CSS3 repeating-radial-gradient()CSSS3
- CSS3 repeating-linear-gradient()CSSS3
- redis3mRedisS3
- 3M互助模式系統開發|3M現成案例模式
- Js的substring和C#的SubstringJSC#
- JavaScript substring()JavaScript