LeetCode in Kotlin

2546. Apply Bitwise Operations to Make Strings Equal

Medium

You are given two 0-indexed binary strings s and target of the same length n. You can do the following operation on s any number of times:

For example, if s = "0110", you can choose i = 0 and j = 2, then simultaneously replace s[0] with (s[0] OR s[2] = 0 OR 1 = 1), and s[2] with (s[0] XOR s[2] = 0 XOR 1 = 1), so we will have s = "1110".

Return true if you can make the string s equal to target, or false otherwise.

Example 1:

Input: s = “1010”, target = “0110”

Output: true

Explanation: We can do the following operations:

Example 2:

Input: s = “11”, target = “00”

Output: false

Explanation: It is not possible to make s equal to target with any number of operations.

Constraints:

Solution

class Solution {
    fun makeStringsEqual(s: String, target: String): Boolean {
        val strLen = s.length
        var ans1 = false
        var ans2 = false
        for (i in 0 until strLen) {
            if (s[i] == '1') {
                ans1 = true
            }
            if (target[i] == '1') {
                ans2 = true
            }
        }
        return ans1 == ans2
    }
}