#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:
相關文章
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- Leetcode 3. Longest Substring Without Repeating CharactersLeetCode
- Leetcode javascript 3 longest-substring-without-repeating-charactersLeetCodeJavaScript
- [LeetCode] 3. Longest Substring Without Repeating Characters 題解LeetCode
- LeetCode Longest Substring Without Repeating Characters(003)解法總結LeetCode
- LeetCode3:Longest Substring Without Repeating Characters(無重複字元的最長子串)LeetCode字元
- Leet Code 3. Longest Substring Without Repeating Characters (最長的沒有重複字元的子字串)字元字串
- 【Leetcode】3. Longest Substring Without RepeatingCharacters無重最長子串LeetCodeGC
- LeetCode 5 (Longest Palindromic Substring)LeetCode
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- SPOJ 1811 Longest Common Substring(字尾自動機)
- Leetcode5: Longest Palindromic Substring(最長迴文子串)LeetCode
- CSS3 repeating-radial-gradient()CSSS3
- CSS3 repeating-linear-gradient()CSSS3
- CSS repeating-radial-gradient()CSS
- CSS repeating-linear-gradient()CSS
- Js的substring和C#的SubstringJSC#
- JavaScript substring()JavaScript
- Longest Valid Parentheses
- SCSS without和withCSS
- Longest Univalue Path
- C# SubstringC#
- Python3解決UnicodeEncodeError: 'ascii' codec can't encode characters in position 0PythonUnicodeErrorASCII
- Performance Without the Event LoopORMOOP
- scp without interative password
- 3M互助模式系統開發|3M現成案例模式
- C#中substringC#
- Substring with Concatenation of All Words
- SQL Server SUBSTRING FunctionsSQLServerFunction
- MySQl 擷取函式 left(),right(),substring(),substring_index() 的用法MySql函式Index
- Leetcode 32 Longest Valid ParenthesesLeetCode
- Leetcode 14 Longest Common PrefixLeetCode
- docker_sshd without passwordDocker
- 687-Longest Univalue Path
- Installing Windows Features without InternetWindows
- LeetCode之Find Common Characters(Kotlin)LeetCodeKotlin
- [LeetCode] 451. Sort Characters By FrequencyLeetCode