Imagine you're a puzzle master creating a word game with pattern matching rules! You have two strings: str1 (the pattern string) and str2 (the target string).
Your goal is to construct the lexicographically smallest string of length n + m - 1 that follows these rules:
- When
str1[i] == 'T': The substring starting at positioniwith lengthmmust equalstr2 - When
str1[i] == 'F': The substring starting at positioniwith lengthmmust NOT equalstr2
Think of it as placing characters one by one while respecting all the pattern constraints. If it's impossible to satisfy all conditions, return an empty string.
Example: If str1 = "TF" and str2 = "ab", you need a string of length 4 where positions 0-1 must be "ab" and positions 1-2 must NOT be "ab".
Input & Output
Visualization
Time & Space Complexity
Generate 26^(n+m-1) strings, each taking O(n*m) time to validate all constraints
Space for storing the current string and str2 for comparisons
Constraints
- 1 โค str1.length โค 103
- 1 โค str2.length โค 103
- str1[i] is either 'T' or 'F'
- str2 consists only of lowercase English letters
- str1.length + str2.length - 1 โค 103