Skip to main content
Back to problems
#422
Easy Algorithms

Valid word square

Array Matrix
42.9% acceptance
Mar 31, 2026
449
274

No description available.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn valid_word_square(words: Vec<String>) -> bool {
    let n = words.len();
    for i in 0..n {
      let row = words[i].as_bytes();
      for (j, &ch) in row.iter().enumerate() {
        // words[j] must exist and have at least i+1 chars matching ch
        if j >= n {
          return false;
        }
        let col_word = words[j].as_bytes();
        if i >= col_word.len() || col_word[i] != ch {
          return false;
        }
      }
    }
    true
  }
}