| comments | true | |||
|---|---|---|---|---|
| difficulty | Medium | |||
| edit_url | https://github.com/doocs/leetcode/edit/main/solution/0000-0099/0003.Longest%20Substring%20Without%20Repeating%20Characters/README_EN.md | |||
| tags |
|
Given a string s, find the length of the longest substring without duplicate characters.
Example 1:
Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Note that"bca"and"cab"are also correct answers.
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 * 104sconsists of English letters, digits, symbols and spaces.
We can use two pointers
Next, we move the right pointer
Finally, we return the answer
The time complexity is
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
cnt = Counter()
ans = l = 0
for r, c in enumerate(s):
cnt[c] += 1
while cnt[c] > 1:
cnt[s[l]] -= 1
l += 1
ans = max(ans, r - l + 1)
return ansclass Solution {
public int lengthOfLongestSubstring(String s) {
int[] cnt = new int[128];
int ans = 0, n = s.length();
for (int l = 0, r = 0; r < n; ++r) {
char c = s.charAt(r);
++cnt[c];
while (cnt[c] > 1) {
--cnt[s.charAt(l++)];
}
ans = Math.max(ans, r - l + 1);
}
return ans;
}
}class Solution {
public:
int lengthOfLongestSubstring(string s) {
int cnt[128]{};
int ans = 0, n = s.size();
for (int l = 0, r = 0; r < n; ++r) {
++cnt[s[r]];
while (cnt[s[r]] > 1) {
--cnt[s[l++]];
}
ans = max(ans, r - l + 1);
}
return ans;
}
};func lengthOfLongestSubstring(s string) (ans int) {
cnt := [128]int{}
l := 0
for r, c := range s {
cnt[c]++
for cnt[c] > 1 {
cnt[s[l]]--
l++
}
ans = max(ans, r-l+1)
}
return
}function lengthOfLongestSubstring(s: string): number {
let ans = 0;
const cnt = new Map<string, number>();
const n = s.length;
for (let l = 0, r = 0; r < n; ++r) {
cnt.set(s[r], (cnt.get(s[r]) || 0) + 1);
while (cnt.get(s[r])! > 1) {
cnt.set(s[l], cnt.get(s[l])! - 1);
++l;
}
ans = Math.max(ans, r - l + 1);
}
return ans;
}impl Solution {
pub fn length_of_longest_substring(s: String) -> i32 {
let mut cnt = [0; 128];
let mut ans = 0;
let mut l = 0;
let chars: Vec<char> = s.chars().collect();
let n = chars.len();
for (r, &c) in chars.iter().enumerate() {
cnt[c as usize] += 1;
while cnt[c as usize] > 1 {
cnt[chars[l] as usize] -= 1;
l += 1;
}
ans = ans.max((r - l + 1) as i32);
}
ans
}
}/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
let ans = 0;
const n = s.length;
const cnt = new Map();
for (let l = 0, r = 0; r < n; ++r) {
cnt.set(s[r], (cnt.get(s[r]) || 0) + 1);
while (cnt.get(s[r]) > 1) {
cnt.set(s[l], cnt.get(s[l]) - 1);
++l;
}
ans = Math.max(ans, r - l + 1);
}
return ans;
};public class Solution {
public int LengthOfLongestSubstring(string s) {
int n = s.Length;
int ans = 0;
var cnt = new int[128];
for (int l = 0, r = 0; r < n; ++r) {
++cnt[s[r]];
while (cnt[s[r]] > 1) {
--cnt[s[l++]];
}
ans = Math.Max(ans, r - l + 1);
}
return ans;
}
}class Solution {
function lengthOfLongestSubstring($s) {
$n = strlen($s);
$ans = 0;
$cnt = array_fill(0, 128, 0);
$l = 0;
for ($r = 0; $r < $n; ++$r) {
$cnt[ord($s[$r])]++;
while ($cnt[ord($s[$r])] > 1) {
$cnt[ord($s[$l])]--;
$l++;
}
$ans = max($ans, $r - $l + 1);
}
return $ans;
}
}class Solution {
func lengthOfLongestSubstring(_ s: String) -> Int {
let n = s.count
var ans = 0
var cnt = [Int](repeating: 0, count: 128)
var l = 0
let sArray = Array(s)
for r in 0..<n {
cnt[Int(sArray[r].asciiValue!)] += 1
while cnt[Int(sArray[r].asciiValue!)] > 1 {
cnt[Int(sArray[l].asciiValue!)] -= 1
l += 1
}
ans = max(ans, r - l + 1)
}
return ans
}
}class Solution {
fun lengthOfLongestSubstring(s: String): Int {
val n = s.length
var ans = 0
val cnt = IntArray(128)
var l = 0
for (r in 0 until n) {
cnt[s[r].toInt()]++
while (cnt[s[r].toInt()] > 1) {
cnt[s[l].toInt()]--
l++
}
ans = Math.max(ans, r - l + 1)
}
return ans
}
}int lengthOfLongestSubstring(char* s) {
int freq[256] = {0};
int l = 0, r = 0;
int ans = 0;
int len = strlen(s);
for (r = 0; r < len; r++) {
char c = s[r];
freq[(unsigned char) c]++;
while (freq[(unsigned char) c] > 1) {
freq[(unsigned char) s[l]]--;
l++;
}
if (ans < r - l + 1) {
ans = r - l + 1;
}
}
return ans;
}