Medium
You are given two strings s1
and s2
, both of length n
, consisting of lowercase English letters.
You can apply the following operation on any of the two strings any number of times:
i
and j
such that i < j
and the difference j - i
is even, then swap the two characters at those indices in the string.Return true
if you can make the strings s1
and s2
equal, andfalse
otherwise.
Example 1:
Input: s1 = “abcdba”, s2 = “cabdab”
Output: true
Explanation: We can apply the following operations on s1:
Example 2:
Input: s1 = “abe”, s2 = “bea”
Output: false
Explanation: It is not possible to make the two strings equal.
Constraints:
n == s1.length == s2.length
1 <= n <= 105
s1
and s2
consist only of lowercase English letters.class Solution {
fun checkStrings(s1: String, s2: String): Boolean {
return check(0, s1, s2) && check(1, s1, s2)
}
fun check(start: Int, s1: String, s2: String): Boolean {
val step = 2
val buf = IntArray(26)
var i = start
while (i < s1.length) {
buf[s1[i].code - 'a'.code]++
buf[s2[i].code - 'a'.code]--
i += step
}
for (j in buf) {
if (j != 0) {
return false
}
}
return true
}
}