Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Check If String Is a Prefix of Array

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1961" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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. Example 1: Input: s = "iloveleetcode", words = ["i","love","leetcode","apples"] Output: true Explanation: s can be made by concatenating "i", "love", and "leetcode" together. Example 2: Input: s = "iloveleetcode", words = ["apples","i","love","leetcode"] Output: false Explanation: It is impossible to make s using a prefix of arr. Constraints: 1 <= words.length <= 100 1 <= words[i].length <= 20 1 <= s.length <= 1000 words[i] and s consist of only lowercase English letters.

Explanation

Here's a breakdown of the solution:

  • Iterative Concatenation: The solution iterates through the words array, concatenating the strings one by one.
  • Prefix Check: After each concatenation, it checks if the concatenated string equals s. If it does, it means s is a prefix string.
  • Early Exit: If the concatenated string becomes longer than s, it's impossible for s to be a prefix, so the function immediately returns False.

  • Runtime Complexity: O(nm) where n is the number of words and m is the average length of the words. *Storage Complexity: O(1) (excluding the input strings).

Code

    def isPrefixString(s: str, words: list[str]) -> bool:
    """
    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.

    Example 1:
    Input: s = "iloveleetcode", words = ["i","love","leetcode","apples"]
    Output: true
    Explanation: s can be made by concatenating "i", "love", and "leetcode" together.

    Example 2:
    Input: s = "iloveleetcode", words = ["apples","i","love","leetcode"]
    Output: false
    Explanation: It is impossible to make s using a prefix of arr.

    Constraints:
    1 <= words.length <= 100
    1 <= words[i].length <= 20
    1 <= s.length <= 1000
    words[i] and s consist of only lowercase English letters.
    """
    prefix = ""
    for word in words:
        prefix += word
        if prefix == s:
            return True
        if len(prefix) > len(s):
            return False
    return False

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Check If String Is a Prefix of Array