leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 28502983118aa4df2e4ce7db1dcf5a69a2f24d21
parent 1beabc3318aefd09d63ea07dd3002be2f92777fd
Author: Jack Sangdahl <jack@pngu.org>
Date:   Sat, 28 Mar 2026 20:39:19 -0600

2839. check if strings can be made equal with operation 1

Diffstat:
Acheck_str_eq_op1.c | 28++++++++++++++++++++++++++++
Acheck_str_eq_op1.py | 20++++++++++++++++++++
2 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/check_str_eq_op1.c b/check_str_eq_op1.c @@ -0,0 +1,28 @@ +// 2839. Check if Strings Can be Made Equal With Operations I +#include "leetcode.h" + +/* + * 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: choose any two indices 'i' and 'j' such + * that 'j - i = 2' then swap the two characters at those indices in the string. + * return true if you make the strings 's1' and 's2' equal and false otherwise. + */ + +bool canBeEqual(char *s1, char *s2) { + return ((s1[0] == s2[0] && s1[2] == s2[2]) || + (s1[0] == s2[2] && s1[2] == s2[0])) && + ((s1[1] == s2[1] && s1[3] == s2[3]) || + (s1[1] == s2[3] && s1[3] == s2[1])); +} + +int main() { + char *s11 = "abcd", *s21 = "cdab"; + char *s12 = "abcd", *s22 = "dacb"; + bool r1 = canBeEqual(s11, s21); + bool r2 = canBeEqual(s12, s22); + printf("%d\n", r1); + assert(r1 == true); + printf("%d\n", r1); + assert(r2 == false); +} diff --git a/check_str_eq_op1.py b/check_str_eq_op1.py @@ -0,0 +1,20 @@ +# 2839. Check if Strings Can be Made Equal With Operations I + +""" +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: choose any two indices 'i' and 'j' such +that 'j - i = 2' then swap the two characters at those indices in the string. +return true if you make the strings 's1' and 's2' equal and false otherwise. +""" + + +class Solution: + def canBeEqual(self, s1: str, s2: str) -> bool: + return {s1[0], s1[2]} == {s2[0], s2[2]} and {s1[1], s1[3]} == {s2[1], s2[3]} + + +if __name__ == "__main__": + obj = Solution() + print(obj.canBeEqual(s1="abcd", s2="cdab")) + print(obj.canBeEqual(s1="abcd", s2="dacb"))