Skip to main content
Back to problems
#1408
Easy Algorithms

String matching in an array

Array String String Matching
69.8% acceptance
Feb 25, 2026
1497
131
Given an array of string words, return all strings in words that are a substring of another word. You can return the answer in any order.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn string_matching(words: Vec<String>) -> Vec<String> {
    let mut result = vec![];
    for i in 0..words.len() {
      for j in 0..words.len() {
        if i != j && words[j].contains(words[i].as_str()) {
          result.push(words[i].clone());
          break;
        }
      }
    }
    result
  }
}