Kotlin: O(n) solution using HashSet and index
Intuition\n Describe your first thoughts on how to solve this problem. \nif the distance between the current index and the swapable index is 2, we should check 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nif the distance between the current index and the swapable index is 2, we should check for a swapable character that is 2 away. If that does not match the char at the current index in s2 fail fast.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInitial condition, s1 and s2 are the same length.\n\nUsing a string builder we construct a new string that we can compare the characters at the end to ensure we have a matching string. When appending characters to the string builder in the order that should match s2.\nThe index loops over the values of s1 and s2. when there is a mismatch, we check for the end of the string before using the s1[index+2] value for comparison. When an s1[index+2] char matches s2[index] we consider that a swap index and add it to the hashSet. \nFor each iteration, when selecting the current char we check for the index in the hashset. If present we select the index - 2 char since it was swapped with that char. \nAt the end we convert the string builder to a string and compare with the chars in s2 for the final result.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n# Code\n```kotlin []\nclass Solution {\n    fun canBeEqual(s1: String, s2: String): Boolean {\n        if (s1.lastIndex != s2.lastIndex) {\n            return false\n        }\n\n        var index = 0 \n        val stringBuilder = StringBuilder()\n        val swappedIndex = mutableSetOf<Int>()\n        while (index <= s1.lastIndex) {\n            var currentChar = if (swappedIndex.contains(index) && (index - 2 >= 0)) { s1[index-2] } else { s1[index] }            \n            if (currentChar != s2[index]) {\n                if(index + 2 <= s1.lastIndex && s1[index+2] == s2[index]) {\n                    swappedIndex.add(index+2)\n                    currentChar = s1[index+2]\n                } else {\n                    return false\n                }\n            }            \n\n            stringBuilder.append(currentChar)    \n            index = index.inc()            \n        }              \n        println(stringBuilder.toString())\n        return stringBuilder.toString() == s2\n    }\n}\n```
['Kotlin']