Subsequence With the Minimum Score - Problem
You are given two strings s and t.
You are allowed to remove any number of characters from the string t.
The score of the string is 0 if no characters are removed from the string t, otherwise:
- Let
leftbe the minimum index among all removed characters. - Let
rightbe the maximum index among all removed characters.
Then the score of the string is right - left + 1.
Return the minimum possible score to make t a subsequence of s.
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).
Input & Output
Example 1 — Basic Case
$
Input:
s = "abacaba", t = "bzc"
›
Output:
2
💡 Note:
Remove "bz" from t, leaving "c". The character "c" exists in s as a subsequence. Score = 1 - 0 + 1 = 2.
Example 2 — Already Subsequence
$
Input:
s = "abcdef", t = "ace"
›
Output:
0
💡 Note:
t is already a subsequence of s, so no characters need to be removed. Score = 0.
Example 3 — Remove Middle
$
Input:
s = "xyz", t = "xay"
›
Output:
1
💡 Note:
Remove "a" from position 1, leaving "xy" which is a subsequence of "xyz". Score = 1 - 1 + 1 = 1.
Constraints
- 1 ≤ s.length, t.length ≤ 105
- s and t consist of only lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
Two strings s and t
2
Find Removal
Identify minimum substring to remove from t
3
Check Subsequence
Verify remaining chars form subsequence of s
Key Takeaway
🎯 Key Insight: Match as much as possible from both ends of t with s, then remove the minimum middle section
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code