leetcode

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

commit 337963533fc481a12482a6524b197e71084087ce
parent 28502983118aa4df2e4ce7db1dcf5a69a2f24d21
Author: Jack Sangdahl <jack@pngu.org>
Date:   Sun, 29 Mar 2026 20:34:18 -0600

2840. check if strings can be made equal with operations 2

Diffstat:
Acheck_str_eq_op2.c | 35+++++++++++++++++++++++++++++++++++
Acheck_str_eq_op2.py | 24++++++++++++++++++++++++
2 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/check_str_eq_op2.c b/check_str_eq_op2.c @@ -0,0 +1,35 @@ +// 2840. Check if Strings Can be Made Equal With Operations II +#include "leetcode.h" + +/* + * 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. choose any two indices '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, otherwise return false. + */ + +bool checkStrings(char *s1, char *s2) { + int n = strlen(s1), cnts[256] = {0}; + for (int i = 0; i < n; i++) { + int offset = (i & 1) << 7; + cnts[offset + s1[i]]++; + cnts[offset + s2[i]]--; + } + for (int i = 0; i < 256; i++) + if (cnts[i]) + return false; + return true; +} + +int main() { + char *s11 = "abcdba", *s21 = "cabdab"; + char *s12 = "abe", *s22 = "bea"; + bool r1 = checkStrings(s11, s21); + bool r2 = checkStrings(s12, s22); + printf("%d\n", r1); + assert(r1 == true); + printf("%d\n", r2); + assert(r2 == false); +} diff --git a/check_str_eq_op2.py b/check_str_eq_op2.py @@ -0,0 +1,24 @@ +# 2840. Check if Strings Can be Made Equal With Operations II +from collections import Counter + +""" +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. choose any two indices '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, otherwise return false. +""" + + +class Solution: + def checkStrings(self, s1: str, s2: str) -> bool: + return Counter(s1[::2]) == Counter(s2[::2]) and Counter(s1[1::2]) == Counter( + s2[1::2] + ) + + +if __name__ == "__main__": + obj = Solution() + print(obj.checkStrings(s1="abcdba", s2="cabdab")) + print(obj.checkStrings(s1="abe", s2="bea"))