Skip to main content
Back to problems
#242
Easy Algorithms

Valid anagram

Hash Table String Sorting
67.8% acceptance
Jan 12, 2026
14193
471
Given two strings s and t, return true if t is an anagram of s, and false otherwise.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn is_anagram(s: String, t: String) -> bool {
    if s.len() != t.len() {
      return false;
    }
    
    let mut count = [0; 26];
    for (sc, tc) in s.bytes().zip(t.bytes()) {
      count[(sc - b'a') as usize] += 1;
      count[(tc - b'a') as usize] -= 1;
    }
    
    count.iter().all(|&c| c == 0)
  }
}