Given a string s, find the length of the longest substring without repeating characters.
Example 1:
Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Example 4:
Input: s = "" Output: 0
Constraints:
- 0 <= s.length <= 5 * 104
- s consists of English letters, digits, symbols and spaces.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int ans = 0;
unordered_map<char, int> unord;
for (int i = 0, j = 0; j < s.length(); j++)
{
if (unord.find(s[j]) != unord.end())
{
i = max(unord[s[j]], i);
}
ans = max(ans, j - i + 1);
unord[s[j]] = j + 1;
}
return ans;
}
};
'Challenge' 카테고리의 다른 글
Check for pair with a given sum (0) | 2021.09.13 |
---|---|
[LeetCode] Longest Palindromic Substring (0) | 2021.09.12 |
[LeetCode] Median of Two Sorted Arrays (0) | 2021.09.11 |
[LeetCode] Add Two Numbers (0) | 2021.09.02 |
[LeetCode] Two Sum (0) | 2021.08.31 |