https://leetcode.com/problems/scramble-string/

class Solution {
    func isScramble(_ s1: String, _ s2: String) -> Bool {
        guard s1.sorted() == s2.sorted() && s1.count == s2.count else { return false }
        if s1.count <= 1 {
            return s1 == s2
        } else if s1.count == 2 {
            return s1 == s2 || s1 == String(s2.reversed())
        } else {
            for i in 1..<s1.count {
                let i1 = s1.index(s1.startIndex, offsetBy: i)
                let i2 = s1.index(s1.startIndex, offsetBy: s1.count-i)
                if (isScramble(String(s1[s1.startIndex..<i1]), String(s2[s2.startIndex..<i1]))
                    && isScramble(String(s1[i1..<s1.endIndex]), String(s2[i1..<s2.endIndex]))) ||
                    (isScramble(String(s1[s1.startIndex..<i1]), String(s2[i2..<s2.endIndex]))
                        && isScramble(String(s1[i1..<s1.endIndex]), String(s2[s2.startIndex..<i2]))) {
                    return true
                }
            }
        }
        return false
    }
}

Runtime: 40 ms, faster than 100.00% of Swift online submissions for Scramble String.
Memory Usage: 19.9 MB, less than 100.00% of Swift online submissions for Scramble String.