String Transformation - Problem
You are given two strings s and t of equal length n. You can perform the following operation on the string s:
Remove a suffix of s of length l where 0 < l < n and append it at the start of s.
For example, let s = 'abcd' then in one operation you can remove the suffix 'cd' and append it in front of s making s = 'cdab'.
You are also given an integer k. Return the number of ways in which s can be transformed into t in exactly k operations.
Since the answer can be large, return it modulo 109 + 7.
Input & Output
Example 1 — Basic Transformation
$
Input:
s = "abcd", t = "cdab", k = 1
›
Output:
1
💡 Note:
Remove suffix "cd" (length 2) and append to front: "abcd" → "cdab". There's exactly 1 way to transform s to t in 1 operation.
Example 2 — Multiple Operations
$
Input:
s = "abc", t = "bca", k = 2
›
Output:
1
💡 Note:
One possible path: "abc" → remove "c" → "cab" → remove "ab" → "bca". Need to count all paths with exactly 2 operations.
Example 3 — Impossible Target
$
Input:
s = "abc", t = "xyz", k = 1
›
Output:
0
💡 Note:
Target "xyz" is not a rotation of "abc", so it's impossible to reach regardless of operations.
Constraints
- 2 ≤ s.length ≤ 1000
- t.length = s.length
- 1 ≤ k ≤ 1000
- s and t consist of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
Source string s, target string t, and exactly k operations
2
Operations
Remove suffix and append to front creates rotations
3
Count Ways
Find number of paths from s to t in exactly k steps
Key Takeaway
🎯 Key Insight: Each suffix operation creates a string rotation - use DP to count paths efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code