diff options
-rw-r--r-- | uncommon_words_two_sent.c | 73 | ||||
-rw-r--r-- | uncommon_words_two_sent.py | 27 |
2 files changed, 100 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); +} diff --git a/uncommon_words_two_sent.py b/uncommon_words_two_sent.py new file mode 100644 index 0000000..8f48d8d --- /dev/null +++ b/uncommon_words_two_sent.py @@ -0,0 +1,27 @@ +# 884. Uncommon Words from Two Sentences +from collections import Counter + +""" +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 +""" + + +class Solution(object): + def uncommonFromSentences(self, s1, s2): + """ + :type s1: str + :type s2: str + :rtype: List[str] + """ + c = Counter((s1 + " " + s2).split()) + return [w for w in c if c[w] == 1] + + +if __name__ == "__main__": + obj = Solution() + print(obj.uncommonFromSentences(s1="this apple is sweet", s2="this apple is sour")) + print(obj.uncommonFromSentences(s1="apple apple", s2="banana")) |