Sliding Tokens: Valid String Transformation
StringsAuto-graded · 31 test casesLast updated
Tests two-pointer traversal, sequence invariants, and directional movement constraints with O(n) time and O(1) space.
You are given two strings initial and final of equal length n. Each string contains only the characters 'L', 'R', '.', and 'X'.
'.'represents an empty position'X'represents a fixed obstacle'L'and'R'are movable tokens
Tokens can slide into adjacent empty positions according to these movement rules:
- An
'L'token can slide left any number of positions into empty slots ('.'), but cannot move right - An
'R'token can slide right any number of positions into empty slots ('.'), but cannot move left - No token can move through another token, through an obstacle (
'X'), or occupy the same position as another token
Return true if final can be obtained from initial through a sequence of valid token moves, otherwise return false.
Your solution should run in O(n) time and use O(1) extra space.
Constraints
1 <= len(initial) == len(final) <= 10^5- Both strings contain only
'L','R','.', and'X'
Examples
Example 1
Input: is_transformation_valid('R.L', 'R.L')
Output: True
Example 2
Input: is_transformation_valid('R.L', 'RL.')
Output: True
Example 3
Input: is_transformation_valid('L.X', 'XL.')
Output: False
Hints
Hint 1
Token movements never change the relative order of non-empty characters. Think about what this means for comparing the two strings.
Hint 2
An L token can only decrease its position (move left), and an R token can only increase its position (move right). Use two pointers to match up corresponding tokens.
Hint 3
Skip over all dots and compare the sequences of tokens and obstacles directly. Each token type has a direction constraint on how far it can have moved.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that sliding movements preserve the relative order of all non-empty characters. Since tokens cannot pass through each other or through obstacles, the sequence of L, R, and X characters (ignoring dots) must be identical in both strings.
Additionally, each token type has a directional constraint:
- An
Ltoken can only slide left, so its position can only decrease or stay the same - An
Rtoken can only slide right, so its position can only increase or stay the same
We use two pointers to traverse both strings simultaneously, skipping over empty positions and matching up corresponding tokens and obstacles:
- Advance pointer
ithroughinitialto find the next non-empty character - Advance pointer
jthroughfinalto find the next non-empty character - Verify the characters match
- For
Ltokens, check thati >= j(moved left or stayed) - For
Rtokens, check thati <= j(moved right or stayed) - Both strings must exhaust their non-empty characters at the same time
This works because we're effectively comparing the stripped sequences and verifying that each token's position change respects its movement direction. Obstacles appearing in the same relative order ensures tokens remain within their respective segments.
Correctness
Necessary conditions: If final is reachable, movements never reorder characters, so the non-empty sequences must match. Each L can only decrease its index, and each R can only increase its index.
Sufficient conditions: When all checks pass, we can construct valid moves by processing all L tokens from left to right (moving each to its target), then all R tokens from right to left. The relative order guarantee ensures no collisions occur.
Complexity
Time: O(n) — Each pointer advances at most n positions, and each iteration advances at least one pointer.
Space: O(1) — Only two index variables are used; no auxiliary data structures are created.
def is_transformation_valid(initial: str, final: str) -> bool:
if len(initial) != len(final):
return False
n = len(initial)
i = j = 0
while i < n or j < n:
# Skip empty positions in initial
while i < n and initial[i] == '.':
i += 1
# Skip empty positions in final
while j < n and final[j] == '.':
j += 1
# Both must exhaust simultaneously
if i == n or j == n:
return i == n and j == n
# Non-empty characters must match in order
if initial[i] != final[j]:
return False
# Obstacles match, continue
if initial[i] == 'X':
i += 1
j += 1
continue
# L tokens can only move left: initial position >= final position
if initial[i] == 'L' and i < j:
return False
# R tokens can only move right: initial position <= final position
if initial[i] == 'R' and i > j:
return False
i += 1
j += 1
return True