optimum cpp code for : Replace With First You are given two strings, S S and T T, of…
- User
optimum cpp code for : Replace With First You are given two strings, S S and T T, of lengths N N and M M respectively.
You can perform the following operation on any of the strings:
Replace a substring with its first character. (i.e. “abcabc” -> “abcc”) What is the minimum number of operations to make both of the strings equal? Or determine that it is impossible to make both of the strings equal.
Input Format The first line of input contains a single integer T T denoting the number of testcases.
The first line of each testcase consists of two space-separated integers N N and M M denoting the lengths of the strings S S and T T, respectively.
The second line of each testcase contains the string S S of length N N.
The third line of each testcase contains the string T T of length M M.
Output Format For each testcase, output a single line containing a single integer denoting the minimum number of operations to make the strings equal, or − 1 −1 if it is not possible to do so.
Constraints 1 ≤ T ≤ 1 0 5 1≤T≤10 5
1 ≤ N , M ≤ 2 ⋅ 1 0 5 1≤N,M≤2⋅10 5
Both strings S S and T T consists of lowercase English alphabets only. The sum of N N over all testcases won't exceed 2 ⋅ 1 0 5 2⋅10 5 . The sum of M M over all testcases won't exceed 2 ⋅ 1 0 5 2⋅10 5 . Sample 1: Input Output 3 4 3 aabc aab 8 8 accepted accapted 1 1 z z 1 2 0 Explanation: Test case 1 1:
You can make S S equal to T T using one operation on S S: "aabc" -> "aab" Test case 2 2:
It can be proven that you need at least two operations to make the strings equal. One possible way is as follows: Perform an operation on S S: "accepted" -> "accpted" Perform an operation on T T: "accapted" -> "accpted" Test case 3 3:
Both strings are already equal.
- Assistant