Skip to main content
Back to problems
#1961
Easy Algorithms

Check if string is a prefix of array

Array Two Pointers String
52.7% acceptance
Feb 25, 2026
555
109
Given a string s and an array of strings words, determine whether s is a prefix string of words. A string s is a prefix string of words if s can be made by concatenating the first k strings in words for some positive k no larger than words.length. Return true if s is a prefix string of words, or false otherwise.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn is_prefix_string(s: String, words: Vec<String>) -> bool {
    let mut concat = String::new();
    for word in &words {
      concat.push_str(word);
      if concat == s {
        return true;
      }
      if concat.len() > s.len() {
        return false;
      }
    }
    false
  }
}