leetcode - 1055. Shortest Way to Form String

Description

A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., “ace” is a subsequence of “abcde” while “aec” is not).

Given two strings source and target, return the minimum number of subsequences of source such that their concatenation equals target. If the task is impossible, return -1.

Example 1:

Input: source = "abc", target = "abcbc"
Output: 2
Explanation: The target "abcbc" can be formed by "abc" and "bc", which are subsequences of source "abc".

Example 2:

Input: source = "abc", target = "acdbc"
Output: -1
Explanation: The target string cannot be constructed from the subsequences of source string due to the character "d" in target string.

Example 3:

Input: source = "xyz", target = "xzyxz"
Output: 3
Explanation: The target string can be constructed as follows "xz" + "y" + "xz".

Constraints:

1 <= source.length, target.length <= 1000
source and target consist of lowercase English letters.

Solution

DP (TLE)

Use dp[i] to denote the minimum number of subsequence we need to form target[:i], then the transformation equation is:
dp[i]=min⁡(dp[k−1])+1,∀k  that target[k:i] is a subsequence of source dp[i] = \min(dp[k - 1]) + 1, \forall k \; \text{that }target[k:i] \text{ is a subsequence of source} dp[i]=min(dp[k1])+1,kthat target[k:i] is a subsequence of source

Time complexity: o(target.len2∗source.len)=o(n3)o(target.len^2*source.len)=o(n^3)o(target.len2source.len)=o(n3)
Space complexity: o(target.len)o(target.len)o(target.len)

Greedy

Go through target, and if the current character is not the same as source, move the pointer in source one step forward. Start over when it’s the end of source.

Time complexity: o(target.len∗source.len)o(target.len*source.len)o(target.lensource.len)
Space complexity: o(1)o(1)o(1)

Code

DP (TLE)

class Solution:
    def shortestWay(self, source: str, target: str) -> int:
        def is_subsequence(source: str, target: str) -> bool:
            i, j = 0, 0
            while i < len(source) and j < len(target):
                if source[i] == target[j]:
                    i += 1
                    j += 1
                else:
                    i += 1
            return j >= len(target)
        if set(target) - set(source):
            return -1
        dp = [i + 1 for i in range(len(target))]
        for i in range(1, len(target)):
            for k in range(i + 1):
                if is_subsequence(source, target[k: i + 1]):
                    dp[i] = min(dp[i], 1 + (dp[k - 1] if k - 1 >= 0 else 0))
        return dp[-1]

Greedy

class Solution:
    def shortestWay(self, source: str, target: str) -> int:
        if set(target) - set(source):
            return -1
        source_index, target_index = 0, 0
        res = 0
        while target_index < len(target):
            if target[target_index] == source[source_index]:
                target_index += 1
            source_index += 1
            if source_index == len(source):
                source_index = 0
                res += 1
        return res + (1 if source_index != 0 else 0)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值