aboutsummaryrefslogtreecommitdiff
path: root/determine_two_str_close.c
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /determine_two_str_close.c
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'determine_two_str_close.c')
-rw-r--r--determine_two_str_close.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/determine_two_str_close.c b/determine_two_str_close.c
new file mode 100644
index 0000000..39b6d0a
--- /dev/null
+++ b/determine_two_str_close.c
@@ -0,0 +1,45 @@
+// 1657. Determine if Two Strings Are Close
+#include "leetcode.h"
+
+/*
+ * two strings are considered close if you can attain one from the other using
+ * the following operations. operation 1:swap any two existing characters.
+ * operation 2: transform every occurence of one exiting character into another
+ * existing character, and do the same with the other character. you can use the
+ * operations on either string as many times as necessary.given two strings 'w1'
+ * and 'w2', return true if 'w1' and 'w2' are close and false otherwise.
+ */
+
+int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }
+
+bool closeStrings(char *w1, char *w2) {
+ int n1 = strlen(w1), n2 = strlen(w2);
+ if (n1 != n2)
+ return false;
+ int *a1 = calloc(26, sizeof(int)), *a2 = calloc(26, sizeof(int));
+ for (int i = 0; i < n1; i++) {
+ a1[w1[i] - 'a']++;
+ a2[w2[i] - 'a']++;
+ }
+ for (int i = 0; i < 26; i++) {
+ if (a1[i] == 0 && a2[i] != 0)
+ return false;
+ if (a2[i] == 0 && a1[i] != 0)
+ return false;
+ }
+ qsort(a1, 26, sizeof(int), cmp);
+ qsort(a2, 26, sizeof(int), cmp);
+ for (int i = 0; i < 26; i++)
+ if (a1[i] != a2[i])
+ return false;
+ return true;
+}
+
+int main() {
+ char *w11 = "abc", *w21 = "bca";
+ char *w12 = "a", *w22 = "aa";
+ char *w13 = "cabbba", *w23 = "abbccc";
+ printf("%d\n", closeStrings(w11, w21)); // expect: 1
+ printf("%d\n", closeStrings(w12, w22)); // expect: 0
+ printf("%d\n", closeStrings(w13, w23)); // expect: 1
+}