Skip to content

Commit

Permalink
refresh
Browse files Browse the repository at this point in the history
refresh
  • Loading branch information
strengthen committed Jun 9, 2019
1 parent 25e3592 commit acba968
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 10 deletions.
16 changes: 15 additions & 1 deletion C++/1078.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
__________________________________________________________________________________________________

class Solution {
public:
vector<string> findOcurrences(string text, string first, string second) {
vector<string> 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;
}
};
__________________________________________________________________________________________________

__________________________________________________________________________________________________
22 changes: 21 additions & 1 deletion C++/1079.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
__________________________________________________________________________________________________

__________________________________________________________________________________________________
18 changes: 17 additions & 1 deletion C++/1080.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
__________________________________________________________________________________________________

__________________________________________________________________________________________________
23 changes: 22 additions & 1 deletion C++/1081.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
__________________________________________________________________________________________________

class Solution {
public:
int fact[8] = { 1, 1, 2, 6, 24, 120, 720, 5040 };
unordered_set<string> 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;
}
};
__________________________________________________________________________________________________

__________________________________________________________________________________________________
18 changes: 17 additions & 1 deletion Java/1078.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
__________________________________________________________________________________________________

__________________________________________________________________________________________________
21 changes: 20 additions & 1 deletion Java/1079.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
__________________________________________________________________________________________________

__________________________________________________________________________________________________
40 changes: 39 additions & 1 deletion Java/1080.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
__________________________________________________________________________________________________

class Solution {
public String smallestSubsequence(String text) {
char[] arr = text.toCharArray();

Map<Character, Integer> map = new HashMap<>();
Set<Character> seen = new HashSet<>();

for (char c : arr) {
map.put(c, map.getOrDefault(c, 0) + 1);
}

Stack<Character> 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();
}
}
__________________________________________________________________________________________________

__________________________________________________________________________________________________
20 changes: 19 additions & 1 deletion Java/1081.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
__________________________________________________________________________________________________

__________________________________________________________________________________________________
Binary file modified MD/Image/LeetCode.xlsx
Binary file not shown.
28 changes: 28 additions & 0 deletions Python3/1078.py
Original file line number Diff line number Diff line change
@@ -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
__________________________________________________________________________________________________

__________________________________________________________________________________________________
15 changes: 15 additions & 0 deletions Python3/1079.py
Original file line number Diff line number Diff line change
@@ -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
__________________________________________________________________________________________________

__________________________________________________________________________________________________
18 changes: 17 additions & 1 deletion Python3/1080.py
Original file line number Diff line number Diff line change
@@ -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)
__________________________________________________________________________________________________

__________________________________________________________________________________________________
7 changes: 6 additions & 1 deletion Python3/1081.py
Original file line number Diff line number Diff line change
@@ -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
__________________________________________________________________________________________________

__________________________________________________________________________________________________

0 comments on commit acba968

Please sign in to comment.