From 5a6cba4d25e6bf80c57aa0b65778a947a0d940f1 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Tue, 4 Feb 2025 22:31:36 -0700 Subject: 1790. check if one string swap can make strings equal --- check_string_swap_eq.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 check_string_swap_eq.c (limited to 'check_string_swap_eq.c') diff --git a/check_string_swap_eq.c b/check_string_swap_eq.c new file mode 100644 index 0000000..e0591a1 --- /dev/null +++ b/check_string_swap_eq.c @@ -0,0 +1,32 @@ +// 1790. Check if One String Swap Can Make Strings Equal +#include "leetcode.h" + +/* + * you are given two strings 's1' and 's2' of equal length. a string swap is an + * operation where you choose two indices in a string (not necessarily + * different) and swap the characters at these indices. return 'true' if it is + * possible to make both strings equal by performing at most one string swap on + * exactly one of the strings. otherwise, return 'false'. + */ + +bool areAlmostEqual(char *s1, char *s2) { + int n = strlen(s1), cnt = 0, idx1 = 0, idx2 = 0; + for (int i = 0; i < n; i++) + if (s1[i] != s2[i]) { + cnt++; + if (!idx1) + idx1 = i; + else + idx2 = i; + } + return (!cnt || cnt == 2) && s1[idx1] == s2[idx2] && s1[idx2] == s2[idx1]; +} + +int main() { + char *s11 = "bank", *s21 = "kanb"; + char *s12 = "attack", *s22 = "defend"; + char *s13 = "kelb", *s23 = "kelb"; + printf("%d\n", areAlmostEqual(s11, s21)); // expect: 1 + printf("%d\n", areAlmostEqual(s12, s22)); // expect: 0 + printf("%d\n", areAlmostEqual(s13, s23)); // expect: 1 +} -- cgit v1.2.3