This repository contains solutions to coding problems from LeetCode, implemented in Rust.
The goal is to practice problem-solving skills, improve Rust proficiency, and maintain high code quality.
- Use
String::as_bytesto efficiently access characters in a string. - Use bitwise flags for tracking multiple boolean states:
let mut flags = 0_u32; flags |= 1 << i; // Set flag i flags ^= 1 << i; // Clear flag i
- Use array indexing instead of HashMap for small fixed-size data sets to improve performance:
let mut counts = [0; 26]; // For lowercase letters 'a' to 'z' for &b in s.into_bytes() { counts[(b - b'a') as usize] += 1; }
- Using linear search in small arrays is efficient enough and simple. No need for complex data structures.
- Use bitwise operations to count unique characters in a string:
let mut mask = 0_u32; for &b in s.into_bytes() { mask |= 1 << (b - b'a'); } let unique_count = mask.count_ones();
- Use faster hasher:
let mut map: HashMap<_, _, _> = HashMap::with_capacity_and_hasher(26, BuildHasherDefault::<DefaultHasher>::default());