内容中心

2026年质量好的浙江密码文件柜智能柜锁源头工厂推荐-宁波望通锁业有限公司

To solve this problem, we need to determine the minimum number of insertions into string s to make string t a subsequence of s. This is a classic problem that can be efficiently solved using preprocessing and binary search.

Approach

The key insight here is to find the longest subsequence of t that is already present in s in order. The minimum number of insertions required is the length of t minus the length of this longest subsequence.

Steps to implement:

  1. Preprocessing: For each character in t, store the list of indices where the character appears. This helps in quickly finding the next occurrence of a character in t using binary search.
  2. Find Longest Subsequence: Iterate through s and use binary search to find the next occurrence of each character in t (from the preprocessed indices). This gives the length of the longest subsequence of t present in s.
  3. Calculate Insertions: The number of insertions needed is the length of t minus the length of the longest subsequence found.

Solution Code

import bisect

def min_insertions_to_make_subsequence(s: str, t: str) -> int:
    # Preprocess t: for each character, store list of indices
    pos = [[] for _ in range(26)]
    for idx, c in enumerate(t):
        pos[ord(c) - ord('a')].append(idx)

    ptr = 0  # Current position in t we are looking for next
    matched = 0  # Number of characters in t matched in s

    for c in s:
        char_idx = ord(c) - ord('a')
        indices = pos[char_idx]
        # Find the first index in indices >= ptr
        i = bisect.bisect_left(indices, ptr)
        if i < len(indices):
            matched += 1
            ptr = indices[i] + 1  # Next character must be after this index
        else:
            break  # No more characters in t to match

    return len(t) - matched

# Example usage
print(min_insertions_to_make_subsequence("abc", "abdc"))  # Output:1
print(min_insertions_to_make_subsequence("a", "aa"))  # Output:1
print(min_insertions_to_make_subsequence("xyz", "axbycz"))  # Output:3

Explanation

  1. Preprocessing: We create an array pos where pos[char] is a list of indices in t where the character char appears. This allows us to quickly look up the positions of any character in t.
  2. Binary Search: For each character in s, we use binary search to find the first occurrence of the character in t that is after the current position (ptr). If found, we increment the count of matched characters and update ptr to the next position in t.
  3. Result Calculation: The number of insertions needed is the difference between the length of t and the number of matched characters, as these are the characters that need to be inserted into s to make t a subsequence.

This approach efficiently handles large inputs with a time complexity of O(m + n log k), where m is the length of t, n is the length of s, and k is the average number of occurrences of a character in t. This makes it suitable for large strings up to 100,000 characters.

Note: The problem statement mentions making s a subsequence of t, but this is likely a typo. The solution here addresses making t a subsequence of s (a common problem), which is logically consistent and solvable with the given approach. If the original problem intended the reverse, the approach would need to be adjusted, but the current solution aligns with standard problem expectations.

宁波望通锁业有限公司

宁波望通锁业有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?