add initial data
This commit is contained in:
1 parent
048a82b101
commit
51d935d06b
66 files changed
+1329
No files matched your search
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "longest-substring-without-repeating-characters"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "longest-substring-without-repeating-characters"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,58 @@
|
||||
fn main() {}
|
||||
|
||||
fn length_of_longest_substring(s: String) -> i32 {
|
||||
if s.is_empty() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut char_idx_map = [-1_i64; 128];
|
||||
|
||||
let mut left: usize = 0;
|
||||
let mut right: usize = 1;
|
||||
|
||||
let mut best = 0;
|
||||
|
||||
let bytes = s.as_bytes();
|
||||
|
||||
while right <= s.len() {
|
||||
let last_char = bytes[right - 1];
|
||||
|
||||
let last_seen = char_idx_map[last_char as usize];
|
||||
|
||||
if last_seen >= left as i64 {
|
||||
left = (last_seen + 1) as usize;
|
||||
right = right.max(left + 1);
|
||||
} else {
|
||||
best = best.max(right - left);
|
||||
char_idx_map[last_char as usize] = right as i64 - 1;
|
||||
right += 1;
|
||||
}
|
||||
}
|
||||
best as _
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::length_of_longest_substring;
|
||||
|
||||
#[test]
|
||||
fn test1() {
|
||||
assert_eq!(length_of_longest_substring("abcabcbb".to_string()), 3);
|
||||
}
|
||||
#[test]
|
||||
fn test2() {
|
||||
assert_eq!(length_of_longest_substring("bbbbb".to_string()), 1);
|
||||
}
|
||||
#[test]
|
||||
fn test3() {
|
||||
assert_eq!(length_of_longest_substring("pwwkew".to_string()), 3);
|
||||
}
|
||||
#[test]
|
||||
fn test4() {
|
||||
assert_eq!(length_of_longest_substring("".to_string()), 0);
|
||||
}
|
||||
#[test]
|
||||
fn test5() {
|
||||
assert_eq!(length_of_longest_substring("S".to_string()), 1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user