Problem statement

https://binarysearch.com/problems/Repeated-String-Replacement-to-Target/

Solution

Equal to Leetcode 1153 String Transforms Into Another String

Complexity

Time complexity is O(n), space complexity is O(26).

Code

class Solution:
    def solve(self, s1, s2):
        if s1 == s2: return True
        G = {}
        for i1, i2 in zip(s1, s2):
            if i1 in G and G[i1] != i2: return False
            G[i1] = i2
        return len(set(s2)) < 26