LeetCode in Kotlin

2839. Check if Strings Can be Made Equal With Operations I

Easy

You are given two strings s1 and s2, both of length 4, consisting of lowercase English letters.

You can apply the following operation on any of the two strings any number of times:

Return true if you can make the strings s1 and s2 equal, and false otherwise.

Example 1:

Input: s1 = “abcd”, s2 = “cdab”

Output: true

Explanation: We can do the following operations on s1:

Example 2:

Input: s1 = “abcd”, s2 = “dacb”

Output: false

Explanation: It is not possible to make the two strings equal.

Constraints:

Solution

class Solution {
    fun canBeEqual(s1: String, s2: String): Boolean {
        return isOk(s1, s2, 0) && isOk(s1, s2, 1)
    }

    private fun isOk(s1: String, s2: String, i: Int): Boolean {
        val a = s1[i]
        val b = s1[i + 2]
        val c = s2[i]
        val d = s2[i + 2]
        if (a == c && b == d) {
            return true
        }
        return a == d && b == c
    }
}