Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Minimum Number of Valid Strings to Form Target I

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3291" 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

You are given an array of strings words and a string target. A string x is called valid if x is a prefix of any string in words. Return the minimum number of valid strings that can be concatenated to form target. If it is not possible to form target, return -1. Example 1: Input: words = ["abc","aaaaa","bcdef"], target = "aabcdabc" Output: 3 Explanation: The target string can be formed by concatenating: Prefix of length 2 of words[1], i.e. "aa". Prefix of length 3 of words[2], i.e. "bcd". Prefix of length 3 of words[0], i.e. "abc". Example 2: Input: words = ["abababab","ab"], target = "ababaababa" Output: 2 Explanation: The target string can be formed by concatenating: Prefix of length 5 of words[0], i.e. "ababa". Prefix of length 5 of words[0], i.e. "ababa". Example 3: Input: words = ["abcdef"], target = "xyz" Output: -1 Constraints: 1 <= words.length <= 100 1 <= words[i].length <= 5 103 The input is generated such that sum(words[i].length) <= 105. words[i] consists only of lowercase English letters. 1 <= target.length <= 5 103 target consists only of lowercase English letters.

Explanation

Here's the solution:

  • Dynamic Programming: Use dynamic programming to determine the minimum number of valid string concatenations to form the target string up to a given index. dp[i] stores the minimum number of valid strings to form target[:i].
  • Prefix Check: For each index in the target string, iterate through the words to check if any word's prefix matches the substring of the target ending at that index.
  • Base Case and Transition: Initialize dp[0] to 0 (empty string requires 0 valid strings). If a valid prefix is found, update dp[i] using dp[i] = min(dp[i], dp[j] + 1), where j is the starting index of the prefix in target.

  • Runtime Complexity: O(M*N*L), where M is the length of target, N is the number of words, and L is the maximum length of a word.

  • Storage Complexity: O(M), where M is the length of the target string.

Code

    def min_concatenations(words, target):
    n = len(target)
    dp = [float('inf')] * (n + 1)
    dp[0] = 0

    for i in range(1, n + 1):
        for word in words:
            word_len = len(word)
            if i >= word_len and target[i - word_len:i] == word[:word_len]:
                dp[i] = min(dp[i], dp[i - word_len] + 1)

            for j in range(1, len(word)+1):
                if i >= j and target[i-j:i] == word[:j]:
                    dp[i] = min(dp[i], dp[i-j] + 1)

    if dp[n] == float('inf'):
        return -1
    else:
        return dp[n]

More from this blog

C

Chatmagic blog

2894 posts