You are given two strings s1 and s2 of the same length n, and a positive integer k.
In one step you pick an index i with 1 <= i <= n and move the part of s1 that starts at 0-indexed position i to the front, so s1 becomes s1[i:] + s1[:i]. Picking i = n leaves the string unchanged.
Return the number of ways to turn s1 into s2 in exactly k steps. Since the answer can be large, return it modulo 10^9 + 7.
Function Signature
def count_transformations(s1: str, s2: str, k: int) -> int:
Rules
-
A way is a sequence of choices
(i_1, i_2, ..., i_k)
, one index per step, where each
i_j
is between 1 and
n
inclusive.
-
Two ways are different when they pick a different index at any step, even if both produce the same strings along the way.
-
A way counts when the string after the last step equals
s2
. The intermediate strings do not matter.
Constraints
-
1 <= s1.length <= 10^5
-
s1.length == s2.length
-
1 <= k <= 10^9
-
s1
and
s2
consist of lowercase English letters.
Examples
Input: s1 = "abcd", s2 = "cdab", k = 2
Output: 4
The sequences that end at "cdab" are (1, 1), (2, 4), (3, 3) and (4, 2). For instance, i = 1 gives "bcda" and a second i = 1 gives "cdab", while i = 4 followed by i = 2 first leaves "abcd" unchanged and then produces "cdab".
Input: s1 = "ababab", s2 = "ababab", k = 1
Output: 3
The choices i = 2, i = 4 and i = 6 each give "ababab" back. The odd choices give "bababa".
Input: s1 = "abc", s2 = "acb", k = 3
Output: 0
The only strings you can ever reach from "abc" are "abc", "bca" and "cab", so "acb" is unreachable for any k.