Problem
https://leetcode.com/problems/longest-substring-without-repeating-characters/
Hint
You can use a sliding window approach.
Think about what you should do when you find a repeating character.
Solution
Code
class Solution {
public:
int lengthOfLongestSubstring(string s) {
string str;
int answer = 0;
for (char c : s) {
auto index = str.find(c);
if (index != string::npos) {
str += c;
str.erase(0, index+1);
} else {
str += c;
answer = max(answer, (int)str.size());
}
}
return answer;
}
};
Complexity
Time
Time complexity will be O(n^2).
Space
Space complexity will be O(n).
Best Solution (Pinned)
Overall
'알고리즘 문제 > Leetcode' 카테고리의 다른 글
[C++] Leetcode 424. Longest Repeating Character Replacement (Hint, Solution) (0) | 2023.05.18 |
---|---|
[C++] Leetcode 57. Insert Interval (Hint, Solution) (0) | 2023.04.27 |
[C++] Leetcode 23. Merge k Sorted Lists (Hint, Solution) (0) | 2023.04.19 |
[C++] Leetcode 73. Set Matrix Zeroes (Hint, Solution) (0) | 2023.04.07 |
[C++] Leetcode 322. Coin Change (Hint, Solution) (0) | 2023.03.30 |