aboutsummaryrefslogtreecommitdiff
path: root/uncommon_words_two_sent.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-09-16 21:23:07 -0600
committerJack Sangdahl <[email protected]>2024-09-16 21:23:07 -0600
commit0179735400baad20f051b9932534cd9b2ec338e0 (patch)
tree2b48c8f6b7df86b9af758d197a66b294fb9d37e9 /uncommon_words_two_sent.c
parent8850c53cf77d11bc3a4baf25eaf4a8292511c35b (diff)
downloadleetcode-0179735400baad20f051b9932534cd9b2ec338e0.tar.gz
leetcode-0179735400baad20f051b9932534cd9b2ec338e0.zip
884. uncommon words from two sentences
Diffstat (limited to 'uncommon_words_two_sent.c')
-rw-r--r--uncommon_words_two_sent.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/uncommon_words_two_sent.c b/uncommon_words_two_sent.c
new file mode 100644
index 0000000..79ca11a
--- /dev/null
+++ b/uncommon_words_two_sent.c
@@ -0,0 +1,73 @@
+// 884. Uncommon Words from Two Sentences
+#include "leetcode.h"
+
+/*
+ * a sentence is a string of single space separated words where each word
+ * consists of only lowercase letters. a word is uncommon if it appears exactly
+ * once in the sentences, and does not appear in the other sentence. given two
+ * sentences 's1' and 's2', return a list of all the uncommon words. you may
+ * return the answer in any order
+ */
+
+struct word_hash {
+ char str[32];
+ int cnt;
+ UT_hash_handle hh;
+};
+
+char **uncommonFromSentences(char *s1, char *s2, int *returnSize) {
+ struct word_hash *h = NULL, *m = NULL;
+ char *s;
+ int n = 0, start_idx = 0, idx = 0, cnt = 0;
+ for (int i = 0; i < 2; i++) {
+ s = !i ? s1 : s2;
+ n = strlen(s);
+ start_idx = 0;
+ idx = 0;
+ do {
+ if (s[idx] == ' ' || idx == n - 1) {
+ cnt = idx - start_idx;
+ if (idx == n - 1)
+ cnt++;
+ char *curr = (char *)calloc(cnt + 1, sizeof(char));
+ strncpy(curr, &s[start_idx], cnt);
+ curr[cnt] = '\0';
+ HASH_FIND_STR(h, curr, m);
+ if (!m) {
+ m = (struct word_hash *)calloc(1, sizeof(struct word_hash));
+ strncpy(m->str, curr, cnt);
+ HASH_ADD_STR(h, str, m);
+ }
+ m->cnt++;
+ start_idx = idx + 1;
+ }
+ idx++;
+ } while (idx < n);
+ cnt = n - start_idx;
+ }
+ char **ans = (char **)calloc(100, sizeof(char *));
+ int ans_idx = 0;
+ for (m = h; m; m = h->hh.next) {
+ if (m->cnt == 1) {
+ ans[ans_idx] = (char *)calloc(strlen(m->str) + 1, sizeof(char));
+ strcpy(ans[ans_idx++], m->str);
+ }
+ }
+ *returnSize = ans_idx;
+ return ans;
+}
+
+int main() {
+ char *s11 = "this apple is sweet", *s21 = "this apple is sour";
+ char *s12 = "apple apple", *s22 = "banana";
+ int rs1, rs2;
+ char **ufs1 = uncommonFromSentences(s11, s21, &rs1);
+ char **ufs2 = uncommonFromSentences(s12, s22, &rs2);
+ for (int i = 0; i < rs1; i++)
+ printf("%s ", ufs1[i]); // expect: sweet sour
+ printf("\n");
+ for (int i = 0; i < rs2; i++)
+ printf("%s ", ufs2[i]); // expect: banana
+ printf("\n");
+ free(ufs1), free(ufs2);
+}