You are given two strings s and t. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by 1. You can't choose a string if it is empty.
For example:
by applying a move to the string "where", the result is the string "here",
by applying a move to the string "a", the result is an empty string "".
You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.
Write a program that finds the minimum number of moves to make two given strings s and t equal.
Input
The first line of the input contains s. In the second line of the input contains t. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and 2⋅10^5, inclusive.
Output
Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings.
Examples
Input
test
west
Output
2
Input
codeforces
yes
Output
9
Input
test
yes
Output
7
Input
b
ab
Output
1
Note
In the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to "est".
In the second example, the move should be applied to the string "codeforces" 8 times. As a result, the string becomes "codeforces" → "es". The move should be applied to the string "yes" once. The result is the same string "yes" → "es".
In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty.
In the fourth example, the first character of the second string should be deleted.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to find the minimum number of moves: we can delete leftmost characters from either string. Equivalent to: we want to make the two strings equal by removing prefixes from each. That is, we can choose a suffix of s and a suffix of t such that they are equal, and we want to maximize the total length of these suffixes (i.e., maximize the sum of lengths of the resulting strings). But note that after deletions, we have two strings equal. Let the resulting equal string be some string u. Then u is a suffix of s (maybe empty) and also a suffix of t (maybe empty). We want to maximize |u| (since we want to minimize deletions, total deletions = |s| + |t| - 2*|u|). Actually careful: if we choose u as the equal string after deletions, we must delete the prefixes: |s| - |u| from s and |t| - |u| from t. So total moves = (|s| - |u|) + (|t| - |u|) = |s|+|t|-2|u|. So to minimize moves, we maximize |u|, where u is a common suffix of s and t. But is it necessarily a suffix? Because we can only delete from the left, so the resulting string is always a suffix of the original. Yes: after deleting any number of leftmost characters, the remaining string is a suffix of the original (the string that starts from some index i to the end). So the equal string must be a suffix of both s and t. So u must be a common suffix. So the problem reduces to finding the longest common suffix of s and t. But is that always the maximum? Let's test: s="b", t="ab". Longest common suffix? Suffixes: of s: "b", ""; of t: "ab", "b", ""; common suffixes: "b" and "". Longest is "b" length 1, then moves = 1+2-2=1. Indeed answer 1. That matches.
But careful: Could we possibly make them equal by deletions on both strings to something that is not a suffix of both? For example, s="ab", t="ba". Could we delete from left only. To make them equal: If we delete leftmost from s: s becomes "b". t unchanged "ba". Not equal. Delete from t: "a". s "ab" vs "a". Not equal. Delete both: "b" vs "a". Not equal. Delete s twice -> "", t twice -> "", equal. That's suffixes: "" is a suffix of both. So yes it's suffix. So longest common suffix is the answer.
But wait, is it always possible to achieve any common suffix? Yes, by deleting appropriate number of leftmost characters. So answer = |s| + |t| - 2 * length_of_longest_common_suffix(s,t). That's trivial.
But is there any catch? Possibly we could also consider that we can delete from either string multiple times and maybe the resulting equal string could be something that is not exactly a suffix of both if we consider deletions interleaved? Actually the final strings are always suffixes. Because after each deletion, the string shrinks from left; the remaining string is a suffix of the original. So after any sequence of deletions, each string is a suffix of its original. So final equal string must be a suffix of s and a suffix of t. So indeed.
Thus the problem is just to compute the longest common suffix. Constraints: lengths up to 2e5, so we can just iterate from the end and compare characters until mismatch. O(min(len_s, len_t)) time.
But double-check examples: Example 1: s="test", t="west". Compare suffixes: from end: 't' vs 't'? Actually s ends with 't', t ends with 't' -> match; then 's' vs 's'? s has 's'? Actually s: t e s t? Wait "test": characters: index0 t? Actually "test": t e s t? Let's write: 't','e','s','t'? Actually "test" is t e s t? Typically "test" is t e s t, yes. "west" is w e s t. So suffixes: compare from end: both 't' match; previous: s[2] = 's', t[2] = 's' match; previous: s[1] = 'e', t[1] = 'e' match; previous: s[0] = 't', t[0] = 'w' mismatch. So longest common suffix length = 3 ("est"). Moves = 4+4-6=2. OK.
Example 2: s="codeforces", t="yes". Compare from end: s ends with 's', t ends with 's'? "yes" ends with 's'? Actually "yes" is y e s: ends with 's' -> match; previous: s[-2]='e', t[-2]='e'? "yes": second last is 'e', s second last from end: "codeforces": letters: c o d e f o r c e s? Actually "codeforces" is c o d e f o r c e s? Let's check length: "codeforces" length 10: indices:0:c,1:o,2:d,3:e,4:f,5:o,6:r,7:c,8:e,9:s. So suffix: from last: index9 s matches; index8 e vs t[-2] e matches; index7 c vs t[-3]? t length=3, t[-3] = y. So at index7 c vs y mismatch. So longest common suffix length = 2 ("es"). Moves = 10+3-4=9. OK.
Example 3: s="test", t="yes". Compare: suffixes: from end: 't' vs 's'? "test": last 't'; "yes": last 's' mismatch -> no common suffix except possibly empty? So length 0. Moves = 4+3-0=7. OK.
Example 4: s="b", t="ab". Compare: s last 'b', t last 'b' match -> length 1, moves=1+2-2=1. OK.
Thus solution is straightforward.
Implementation: read s, t (strip newline). Let i = len(s)-1, j = len(t)-1. While i>=0 and j>=0 and s[i]==t[j]: i--; j--. Then common suffix length = len(s) - (i+1)? Actually number of matched characters = (len(s)-1 - i) because after loop, i is index of last mismatch or -1 if all matched. Let matched = len(s)-1 - i? Actually if we break because mismatch or one index out of bounds, i and j are the indices before which we have matched all suffix characters. Let's compute: we start with i = len(s)-1, j = len(t)-1. While condition holds, we decrement both. At exit, i and j are indices where characters differ, or one is -1. The number of matched characters = len(s) - (i+1) = len(s)-i-1. Equivalent to original i - final i? Actually originally we matched from end. So common_len = (len(s)-1) - i? Since i started at len(s)-1 and after each match we decrement. After k matches, i becomes len(s)-1 - k, j becomes len(t)-1 - k. Loop stops when condition fails. So number of matches k = len(s)-1 - i (if i ends up as len(s)-1 - k). So common_len = len(s) - (i+1) = len(s)-i-1. Similarly can use len(t)-j-1. Since both same when both strings matched the same number of suffix characters, we can compute min of the two? Actually they should be equal. But careful: if one string is shorter, then we might stop because i or j becomes -1, and then the condition fails because the other string still has characters but we can't compare beyond bounds. However, if we have a short string, the common suffix can't exceed its length. So we should stop when either index becomes -1 (i.e., we matched all characters of the shorter string). In that case, the number of matched characters equals the length of the shorter string if the characters matched all the way; but also we need to check if the characters matched from the end up to that point. The loop condition includes i>=0 && j>=0, so when one index becomes -1, loop exits. At that point, we have matched min(len(s), len(t))? Not exactly: suppose s="abc", t="bc". Compare: i=2('c'), j=1('c') match; i=1('b'), j=0('b') match; then i=0('a'), j=-1, loop condition fails because j<0. So we have matched 2 characters, which is indeed the length of t. So common_len = len(t)-j-1? j=-1 => len(t)-(-1)-1 = len(t) (for t length 2, that gives 2). That's correct. Similarly using s: i=0? After two matches, i=0, but loop stopped because j<0, so common_len = len(s)-i-1 = 3-0-1=2. Works. So we can compute common_len = len(s)-i-1.
Thus answer = len(s)+len(t)-2*common_len.
We'll implement in code.
Edge cases: Strings can be up to 2e5, so O(N) fine.
But is there any nuance? The problem statement: "You are required to make two given strings equal using the fewest number of moves." We can delete from left only. Our reasoning holds. So answer is as described.
Thus final solution: read s and t, compute common suffix length by iterating from the end while characters equal. Print total length minus 2*common.
Let's code.
In Python:
def solve():
import sys
s = sys.stdin.readline().strip()
t = sys.stdin.readline().strip()
i, j = len(s)-1, len(t)-1
while i >= 0 and j >= 0 and s[i] == t[j]:
i -= 1
j -= 1
common = len(s) - i - 1
print(len(s) + len(t) - 2 * common)
Works for examples.
But one more test: if both strings identical: s="abc", t="abc". Then i goes: i=2 match, i=1 match, i=0 match, then i=-1, j=-1 loop exit. common = len(s)-(-1)-1 = 3. Answer = 3+3-6=0, which is correct: no moves needed.
If one is prefix of the other: s="abc", t="bc". As earlier: common=2, answer=3+2-4=1: need to delete first character of s. Indeed.
If both empty? Not possible: strings length at least 1 per constraints.
Thus solution correct.
But wait, is there any scenario where it's optimal to delete more from one string than the other to get a common string that is not a suffix of both? Actually, after deletions, the remaining string is always a suffix. So to have two strings equal, they must be suffixes of their originals. So they must be a common suffix. So maximum length of common suffix yields minimal moves.
Thus answer is straightforward.
Now we'll produce final answer with explanation.
But we need to be careful about input reading: maybe there are spaces? The problem says both strings consist only of lowercase Latin letters, and each line contains a string. So strip newline is fine.
Time complexity O(min(|s|, |t|)) which is fine.
We'll produce final output.
Thus the solution. ```python
import sys
def main() -> None:
s = sys.stdin.readline().strip()
t = sys.stdin.readline().strip()
code
i, j = len(s) - 1, len(t) - 1
while i >= 0 and j >= 0 and s[i] == t[j]:
i -= 1
j -= 1
common = len(s) - i - 1 # length of the longest common suffix
ans = len(s) + len(t) - 2 * common
print(ans)