comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
Medium |
|
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.
Constraints:
0 <= s.length <= 5 * 104
s
consists of English letters, digits, symbols and spaces.
Define a hash table to record the characters in the current window. Let ans
.
For each character s
, we call it c
. Then we add c
to the hash table. At this time, the window ans
.
Finally, return ans
.
The time complexity is s
.
Two pointers algorithm template:
for (int i = 0, j = 0; i < n; ++i) {
while (j < i && check(j, i)) {
++j;
}
// logic of specific problem
}
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
ss = set()
ans = i = 0
for j, c in enumerate(s):
while c in ss:
ss.remove(s[i])
i += 1
ss.add(c)
ans = max(ans, j - i + 1)
return ans
class Solution {
public int lengthOfLongestSubstring(String s) {
boolean[] ss = new boolean[128];
int ans = 0;
for (int i = 0, j = 0; j < s.length(); ++j) {
char c = s.charAt(j);
while (ss[c]) {
ss[s.charAt(i++)] = false;
}
ss[c] = true;
ans = Math.max(ans, j - i + 1);
}
return ans;
}
}
class Solution {
public:
int lengthOfLongestSubstring(string s) {
bool ss[128]{};
int ans = 0;
for (int i = 0, j = 0; j < s.size(); ++j) {
while (ss[s[j]]) {
ss[s[i++]] = false;
}
ss[s[j]] = true;
ans = max(ans, j - i + 1);
}
return ans;
}
};
func lengthOfLongestSubstring(s string) (ans int) {
ss := [128]bool{}
for i, j := 0, 0; j < len(s); j++ {
for ss[s[j]] {
ss[s[i]] = false
i++
}
ss[s[j]] = true
ans = max(ans, j-i+1)
}
return
}
function lengthOfLongestSubstring(s: string): number {
let ans = 0;
const ss: Set<string> = new Set();
for (let i = 0, j = 0; j < s.length; ++j) {
while (ss.has(s[j])) {
ss.delete(s[i++]);
}
ss.add(s[j]);
ans = Math.max(ans, j - i + 1);
}
return ans;
}
use std::collections::HashSet;
impl Solution {
pub fn length_of_longest_substring(s: String) -> i32 {
let s = s.as_bytes();
let mut ss = HashSet::new();
let mut i = 0;
s.iter()
.map(|c| {
while ss.contains(&c) {
ss.remove(&s[i]);
i += 1;
}
ss.insert(c);
ss.len()
})
.max()
.unwrap_or(0) as i32
}
}
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
let ans = 0;
const ss = new Set();
for (let i = 0, j = 0; j < s.length; ++j) {
while (ss.has(s[j])) {
ss.delete(s[i++]);
}
ss.add(s[j]);
ans = Math.max(ans, j - i + 1);
}
return ans;
};
public class Solution {
public int LengthOfLongestSubstring(string s) {
int ans = 0;
var ss = new HashSet<char>();
for (int i = 0, j = 0; j < s.Length; ++j) {
while (ss.Contains(s[j])) {
ss.Remove(s[i++]);
}
ss.Add(s[j]);
ans = Math.Max(ans, j - i + 1);
}
return ans;
}
}
class Solution {
/**
* @param String $s
* @return Integer
*/
function lengthOfLongestSubstring($s) {
$ans = 0;
$ss = [];
for ($i = 0, $j = 0; $j < strlen($s); ++$j) {
while (in_array($s[$j], $ss)) {
unset($ss[array_search($s[$i++], $ss)]);
}
$ss[] = $s[$j];
$ans = max($ans, $j - $i + 1);
}
return $ans;
}
}
class Solution {
func lengthOfLongestSubstring(_ s: String) -> Int {
var map = [Character: Int]()
var currentStartingIndex = 0
var i = 0
var maxLength = 0
for char in s {
if map[char] != nil {
if map[char]! >= currentStartingIndex {
maxLength = max(maxLength, i - currentStartingIndex)
currentStartingIndex = map[char]! + 1
}
}
map[char] = i
i += 1
}
return max(maxLength, i - currentStartingIndex)
}
}
proc lengthOfLongestSubstring(s: string): int =
var
i = 0
j = 0
res = 0
literals: set[char] = {}
while i < s.len:
while s[i] in literals:
if s[j] in literals:
excl(literals, s[j])
j += 1
literals.incl(s[i]) # Uniform Function Call Syntax f(x) = x.f
res = max(res, i - j + 1)
i += 1
result = res # result has the default return value
class Solution {
fun lengthOfLongestSubstring(s: String): Int {
var char_set = BooleanArray(128)
var left = 0
var ans = 0
s.forEachIndexed { right, c ->
while (char_set[c.code]) {
char_set[s[left].code] = false
left++
}
char_set[c.code] = true
ans = Math.max(ans, right - left + 1)
}
return ans
}
}