Longest Substring Without Repeating Characters
PROBLEM
Given a string s
, find the length of the longest substring
without repeating characters.
Example1
*Input*: s = "abcabcbb"
*Output*: 3
Explanation: The answer is "abc", with the length of 3.
Example2
*Input*: s = "bbbbb"
*Output*: 1
Explanation: The answer is "b", with the length of 1.
Example3
*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.
SOLVING
Steps
Code
class Solution {
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int string_size = s.length();
int pointer = 0;
int max_size = 0;
int size;
unordered_map<char, int> hash;
while (pointer < string_size) {
size = 0;
hash.clear();
while (pointer < string_size &&
hash.emplace(s[pointer], pointer).second) {
pointer++;
size++;
}
if (size > max_size) max_size = size;
if (pointer < string_size) pointer = hash[s[pointer]] + 1;
}
return max_size;
}
};