impl Solution {
fn length_of_longest_substring(s: String) -> i32 {
use std::collections::HashSet;
// Create a HashSet to store characters of the current substring.
let mut char_set = HashSet::new();
// Convert the string to a vector of chars to easily access characters by index.
let chars: Vec<char> = s.chars().collect();
// Initialize variables for the maximum length found and the left pointer of the window.
let mut max_length = 0;
let mut left = 0;
// Iterate through the string with the right pointer (i).
for right in 0..chars.len() {
// Check if the character at the right pointer is already in the set.
// If it is, remove characters from the left until it's not, effectively sliding the window.
while char_set.contains(&chars[right]) {
char_set.remove(&chars[left]);
left += 1;
}
// Add the current character to the set.
char_set.insert(chars[right]);
// Calculate the length of the current substring and update max length if necessary.
max_length = std::cmp::max(max_length, right - left + 1);
}
// Return the maximum length found.
max_length as i32
}
}