diff --git a/C++/1078.cpp b/C++/1078.cpp index 433d2ad..1c70196 100644 --- a/C++/1078.cpp +++ b/C++/1078.cpp @@ -1,5 +1,19 @@ __________________________________________________________________________________________________ - +class Solution { +public: + vector findOcurrences(string text, string first, string second) { + vector res; + auto bigram = first + " " + second + " "; + auto p = text.find(bigram); + while (p != string::npos) { + auto p1 = p + bigram.size(), p2 = p1; + while (p2 < text.size() && text[p2] != ' ') ++p2; + res.push_back(text.substr(p1, p2 - p1)); + p = text.find(bigram, p + 1); + } + return res; +} +}; __________________________________________________________________________________________________ __________________________________________________________________________________________________ diff --git a/C++/1079.cpp b/C++/1079.cpp index 433d2ad..af7fbfe 100644 --- a/C++/1079.cpp +++ b/C++/1079.cpp @@ -1,5 +1,25 @@ __________________________________________________________________________________________________ - +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Solution { +public: + TreeNode* sufficientSubset(TreeNode* root, int limit) { + if (root->left == root->right) + return root->val < limit ? NULL : root; + if (root->left) + root->left = sufficientSubset(root->left, limit - root->val); + if (root->right) + root->right = sufficientSubset(root->right, limit - root->val); + return root->left == root->right ? NULL : root; + } +}; __________________________________________________________________________________________________ __________________________________________________________________________________________________ diff --git a/C++/1080.cpp b/C++/1080.cpp index 433d2ad..b703c2b 100644 --- a/C++/1080.cpp +++ b/C++/1080.cpp @@ -1,5 +1,21 @@ __________________________________________________________________________________________________ - +class Solution { +public: + string smallestSubsequence(string s, string res = "") { + int cnt[26] = {}, used[26] = {}; + for (auto ch : s) ++cnt[ch - 'a']; + for (auto ch : s) { + --cnt[ch - 'a']; + if (used[ch - 'a']++ > 0) continue; + while (!res.empty() && res.back() > ch && cnt[res.back() - 'a'] > 0) { + used[res.back() - 'a'] = 0; + res.pop_back(); + } + res.push_back(ch); + } + return res; +} +}; __________________________________________________________________________________________________ __________________________________________________________________________________________________ diff --git a/C++/1081.cpp b/C++/1081.cpp index 433d2ad..4fc943a 100644 --- a/C++/1081.cpp +++ b/C++/1081.cpp @@ -1,5 +1,26 @@ __________________________________________________________________________________________________ - +class Solution { +public: + int fact[8] = { 1, 1, 2, 6, 24, 120, 720, 5040 }; +unordered_set st; +int uniquePerms(string& s) { + int cnt[26] = {}; + for (auto ch : s) ++cnt[ch - 'A']; + auto res = fact[s.size()]; + for (auto n : cnt) res /= fact[n]; + return res; +} +int dfs(string& s, string seq = "", int pos = 0) { + if (pos >= s.size()) { + return st.insert(seq).second ? uniquePerms(seq) : 0; + } + return dfs(s, seq, pos + 1) + dfs(s, seq + s[pos], pos + 1); +} +int numTilePossibilities(string tiles) { + sort(begin(tiles), end(tiles)); + return dfs(tiles) - 1; +} +}; __________________________________________________________________________________________________ __________________________________________________________________________________________________ diff --git a/Java/1078.java b/Java/1078.java index 433d2ad..260776f 100644 --- a/Java/1078.java +++ b/Java/1078.java @@ -1,5 +1,21 @@ __________________________________________________________________________________________________ - +class Solution { + public String[] findOcurrences(String text, String first, String second) { + String[] t = text.split(" "); + int ind = 0; + + //Loop through words, if last two are first and second respectively, then add current to array. + for(int i = 2; i < t.length; i++) + if(t[i-1].equals(second) && t[i-2].equals(first)) t[ind++]=t[i]; + + //Construct and fill output array. + String[] ans = new String[ind]; + for(int i = 0; i < ind; i++) + ans[i]=t[i]; + + return ans; + } +} __________________________________________________________________________________________________ __________________________________________________________________________________________________ diff --git a/Java/1079.java b/Java/1079.java index 433d2ad..3b64c38 100644 --- a/Java/1079.java +++ b/Java/1079.java @@ -1,5 +1,24 @@ __________________________________________________________________________________________________ - +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public TreeNode sufficientSubset(TreeNode root, int limit) { + if (root.left == root.right) + return root.val < limit ? null : root; + if (root.left != null) + root.left = sufficientSubset(root.left, limit - root.val); + if (root.right != null) + root.right = sufficientSubset(root.right, limit - root.val); + return root.left == root.right ? null : root; + } +} __________________________________________________________________________________________________ __________________________________________________________________________________________________ diff --git a/Java/1080.java b/Java/1080.java index 433d2ad..dc85697 100644 --- a/Java/1080.java +++ b/Java/1080.java @@ -1,5 +1,43 @@ __________________________________________________________________________________________________ - +class Solution { + public String smallestSubsequence(String text) { + char[] arr = text.toCharArray(); + + Map map = new HashMap<>(); + Set seen = new HashSet<>(); + + for (char c : arr) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + + Stack stack = new Stack<>(); + + for (char c : arr) { + //we have seen this char + if (seen.contains(c)) { + map.put(c, map.get(c) - 1); + continue; + } + // if the top char is larger than current char, we should remove it + while (!stack.isEmpty() && stack.peek() > c && map.get(stack.peek()) > 1) { + char temp = stack.pop(); + map.put(temp, map.get(temp) - 1); + seen.remove(temp); + } + stack.push(c); + seen.add(c); + } + + StringBuilder sb = new StringBuilder(); + //while (!stack.isEmpty()) sb.insert(0, stack.pop()); + //return sb.toString(); + + + //credit to @yz5548 + while (!stack.isEmpty()) sb.append(stack.pop()); + return sb.reverse().toString(); + } +} __________________________________________________________________________________________________ __________________________________________________________________________________________________ diff --git a/Java/1081.java b/Java/1081.java index 433d2ad..6a238a6 100644 --- a/Java/1081.java +++ b/Java/1081.java @@ -1,5 +1,23 @@ __________________________________________________________________________________________________ - +class Solution { + public int numTilePossibilities(String tiles) { + int[] count = new int[26]; + for (char c : tiles.toCharArray()) count[c - 'A']++; + return dfs(count); + } + + int dfs(int[] arr) { + int sum = 0; + for (int i = 0; i < 26; i++) { + if (arr[i] == 0) continue; + sum++; + arr[i]--; + sum += dfs(arr); + arr[i]++; + } + return sum; + } +} __________________________________________________________________________________________________ __________________________________________________________________________________________________ diff --git a/MD/Image/LeetCode.xlsx b/MD/Image/LeetCode.xlsx index 3f7eca5..82bf2b0 100644 Binary files a/MD/Image/LeetCode.xlsx and b/MD/Image/LeetCode.xlsx differ diff --git a/Python3/1078.py b/Python3/1078.py index 433d2ad..5b9b4be 100644 --- a/Python3/1078.py +++ b/Python3/1078.py @@ -1,5 +1,33 @@ __________________________________________________________________________________________________ +class Solution: + def genNext(self, pattern): + next, j = [0] * len(pattern), -1 + for i in range(len(pattern)): + while j != -1 and pattern[j + 1] != pattern[i]: + j = next[j] + if i and next[j + 1] == pattern[i]: + j += 1 + next[i] = j + return next + def findOcurrences(self, text: str, first: str, second: str) -> List[str]: + pattern, text = "{} {} ".format(first, second), text + " " + next, j, result = self.genNext(pattern), -1, [] + + for i in range(len(text)): + while j != -1 and pattern[j + 1] != text[i]: + j = next[j] + + if pattern[j + 1] == text[i]: + j += 1 + + if j == len(pattern) - 1: + # Corner case: in "aaa good girl" the `girl` is invalid + if (i - len(pattern) == -1 or text[i - len(pattern)] == " ") and i + 1 < len(text): + result.append(text[i + 1:text.index(" ", i + 1)]) + j = next[j] + + return result __________________________________________________________________________________________________ __________________________________________________________________________________________________ diff --git a/Python3/1079.py b/Python3/1079.py index 433d2ad..07dc3d4 100644 --- a/Python3/1079.py +++ b/Python3/1079.py @@ -1,5 +1,20 @@ __________________________________________________________________________________________________ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def sufficientSubset(self, root, limit): + if root.left == root.right is None: + return None if root.val < limit else root + if root.left: + root.left = self.sufficientSubset(root.left, limit - root.val) + if root.right: + root.right = self.sufficientSubset(root.right, limit - root.val) + return root if root.left or root.right else None __________________________________________________________________________________________________ __________________________________________________________________________________________________ diff --git a/Python3/1080.py b/Python3/1080.py index 433d2ad..85b167a 100644 --- a/Python3/1080.py +++ b/Python3/1080.py @@ -1,5 +1,21 @@ __________________________________________________________________________________________________ - +class Solution(object): + def smallestSubsequence(self, text): + """ + :type text: str + :rtype: str + """ + last_idx = dict() + for i, c in enumerate(text): + last_idx[c] = i + stack = [] + for i, c in enumerate(text): + if c in stack: + continue + while stack and c < stack[-1] and last_idx[stack[-1]] > i: + stack.pop() + stack.append(c) + return ''.join(stack) __________________________________________________________________________________________________ __________________________________________________________________________________________________ diff --git a/Python3/1081.py b/Python3/1081.py index 433d2ad..fedd52b 100644 --- a/Python3/1081.py +++ b/Python3/1081.py @@ -1,5 +1,10 @@ __________________________________________________________________________________________________ - +class Solution: + def numTilePossibilities(self, tiles: str) -> int: + res = {''} + for c in tiles: + res |= {d[:i] + c + d[i:] for d in res for i in range(len(d) + 1)} + return len(res) - 1 __________________________________________________________________________________________________ __________________________________________________________________________________________________