Orderly Class
InterviewTime limit2sMemory limit512 MB
Given two equal-length strings A and B, count the intervals in A such that reversing that interval turns A into B.
- Level
Medium6 of 10
- Topics
- String, Two pointers, Implementation, Brute force
- Solved
- No attempts yet
Problem
Ms. Thomas is managing her class of students. She placed all of them in a line and gave the -th student from the left a card with the letter written on it.
She would now like to rearrange the students so that the -th student from the left holds a card with the letter written on it. She has one way to do it. She chooses a group of consecutive students and reverses the order of that group. Every student keeps the original card while the group is reversed.
Count the ways to do this. Two ways are different when the first position or the last position of the chosen group differs. If no single reversal turns into , the answer is 0.
For equal to abba and equal to aabb, the only group is a(bba). For equal to caxcab and equal to cacxab, there are two groups, ca(xc)ab and c(axca)b. For equal to a and equal to z, there is no group to choose.
Input
The first line holds the string and the second line holds the string . Both strings consist of lowercase English letters only. The -th character of is and the -th character of is . The two strings have the same length with , and and are not identical.
Output
Print one integer, the number of ways to reverse a single consecutive group of so that the result is .