LeetCode-in-Java

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

public class Solution {
    public boolean canBeEqual(String s1, String s2) {
        return isOk(s1, s2, 0) && isOk(s1, s2, 1);
    }

    private boolean isOk(String s1, String s2, int i) {
        char a = s1.charAt(i);
        char b = s1.charAt(i + 2);
        char c = s2.charAt(i);
        char d = s2.charAt(i + 2);
        if (a == c && b == d) {
            return true;
        }
        return a == d && b == c;
    }
}