Skip to content

Commit 3b0ebda

Browse files
committed
new soln
1 parent 9333e95 commit 3b0ebda

File tree

4 files changed

+288
-0
lines changed

4 files changed

+288
-0
lines changed

1002.CommonChars.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// 1002. Find Common Characters
2+
// Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.
3+
4+
// Example 1:
5+
// Input: words = ["bella","label","roller"]
6+
// Output: ["e","l","l"]
7+
// Example 2:
8+
// Input: words = ["cool","lock","cook"]
9+
// Output: ["c","o"]
10+
11+
// Constraints:
12+
// 1 <= words.length <= 100
13+
// 1 <= words[i].length <= 100
14+
// words[i] consists of lowercase English letters.
15+
16+
public class Solution {
17+
public IList<string> CommonChars(string[] words) {
18+
int[] minFreq = new int[26];
19+
Array.Fill(minFreq, Int32.MaxValue);
20+
foreach(var word in words){
21+
int[] freq = new int[26];
22+
foreach(char c in word)
23+
freq[c - 'a']++;
24+
for(int i = 0; i < 26; i++)
25+
minFreq[i] = Math.Min(minFreq[i], freq[i]);
26+
}
27+
IList<string> result = new List<string>();
28+
for(int i = 0; i < 26; i++){
29+
if(minFreq[i] < Int32.MaxValue){
30+
for(int j = 0; j < minFreq[i]; j++)
31+
result.Add(((char)('a'+i)).ToString());
32+
}
33+
}
34+
return result;
35+
}
36+
}

1296.IsPossibleDivide.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// 1296. Divide Array in Sets of K Consecutive Numbers
2+
// Given an array of integers nums and a positive integer k, check whether it is possible to divide this array into sets of k consecutive numbers.
3+
// Return true if it is possible. Otherwise, return false.
4+
5+
// Example 1:
6+
// Input: nums = [1,2,3,3,4,4,5,6], k = 4
7+
// Output: true
8+
// Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
9+
// Example 2:
10+
// Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
11+
// Output: true
12+
// Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
13+
// Example 3:
14+
// Input: nums = [1,2,3,4], k = 3
15+
// Output: false
16+
// Explanation: Each array should be divided in subarrays of size 3.
17+
18+
// Constraints:
19+
// 1 <= k <= nums.length <= 105
20+
// 1 <= nums[i] <= 109
21+
22+
public class Solution {
23+
public bool IsPossibleDivide(int[] nums, int k) {
24+
if(nums.Length%k != 0)
25+
return false;
26+
27+
Array.Sort(nums);
28+
29+
Dictionary<int,int> freq = new();
30+
foreach(var num in nums){
31+
if(freq.ContainsKey(num))
32+
freq[num]++;
33+
else
34+
freq[num] = 1;
35+
}
36+
while(freq.Any()){
37+
int num = freq.First().Key;
38+
for(int i = 0; i < k; i++){
39+
int currNum = num + i;
40+
if(freq.ContainsKey(currNum)){
41+
freq[currNum]--;
42+
if(freq[currNum] == 0)
43+
freq.Remove(currNum);
44+
}
45+
else
46+
return false;
47+
}
48+
}
49+
return true;
50+
}
51+
}

648.ReplaceWords.cs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// 648. Replace Words
2+
// In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word derivative. For example, when the root "help" is followed by the word "ful", we can form a derivative "helpful".
3+
// Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the derivatives in the sentence with the root forming it. If a derivative can be replaced by more than one root, replace it with the root that has the shortest length.
4+
// Return the sentence after the replacement.
5+
6+
// Example 1:
7+
// Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
8+
// Output: "the cat was rat by the bat"
9+
// Example 2:
10+
// Input: dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
11+
// Output: "a a b c"
12+
13+
// Constraints:
14+
// 1 <= dictionary.length <= 1000
15+
// 1 <= dictionary[i].length <= 100
16+
// dictionary[i] consists of only lower-case letters.
17+
// 1 <= sentence.length <= 106
18+
// sentence consists of only lower-case letters and spaces.
19+
// The number of words in sentence is in the range [1, 1000]
20+
// The length of each word in sentence is in the range [1, 1000]
21+
// Every two consecutive words in sentence will be separated by exactly one space.
22+
// sentence does not have leading or trailing spaces.
23+
24+
//Trie
25+
public class Solution {
26+
Trie root = new Trie();
27+
28+
public string ReplaceWords(IList<string> dictionary, string sentence) {
29+
InsertPrefix(dictionary);
30+
return Convert(sentence);
31+
}
32+
33+
public void InsertPrefix(IList<string> dictionary) {
34+
foreach (var word in dictionary) {
35+
Trie node = root;
36+
foreach (var ch in word) {
37+
if (!node.Contains(ch))
38+
node.Insert(ch);
39+
node = node.child[ch - 'a'];
40+
}
41+
node.SetEnd();
42+
}
43+
}
44+
45+
public string Convert(string sentence) {
46+
List<string> words = sentence.Split(" ").ToList();
47+
StringBuilder result = new StringBuilder();
48+
49+
foreach (var word in words) {
50+
StringBuilder prefix = new StringBuilder();
51+
Trie node = root;
52+
bool found = false;
53+
54+
foreach (var ch in word) {
55+
if (node.Contains(ch)) {
56+
prefix.Append(ch);
57+
node = node.child[ch - 'a'];
58+
if (node.IsEnd()) {
59+
found = true;
60+
break;
61+
}
62+
} else {
63+
break;
64+
}
65+
}
66+
67+
if (found) {
68+
result.Append(prefix);
69+
} else {
70+
result.Append(word);
71+
}
72+
result.Append(" ");
73+
}
74+
75+
// Remove the extra space at the end
76+
if (result.Length > 0) {
77+
result.Length--;
78+
}
79+
80+
return result.ToString();
81+
}
82+
83+
public class Trie {
84+
public Trie[] child;
85+
bool isEnd;
86+
87+
public Trie() {
88+
child = new Trie[26];
89+
isEnd = false;
90+
}
91+
92+
public void Insert(char ch) {
93+
child[ch - 'a'] = new Trie();
94+
}
95+
96+
public bool Contains(char ch) {
97+
return child[ch - 'a'] != null;
98+
}
99+
100+
public bool IsEnd() {
101+
return isEnd;
102+
}
103+
104+
public void SetEnd() {
105+
isEnd = true;
106+
}
107+
}
108+
}
109+
110+
111+
//HashSet
112+
public class Solution {
113+
public string ReplaceWords(IList<string> dictionary, string sentence) {
114+
StringBuilder word = new StringBuilder("");
115+
HashSet<string> wordSet = new HashSet<string>(dictionary);
116+
StringBuilder result = new StringBuilder("");;
117+
for(int c = 0; c < sentence.Length; c++){
118+
if(sentence[c] != ' '){
119+
word.Append(sentence[c]);
120+
if(wordSet.Contains(word.ToString())){
121+
while(c+1 < sentence.Length && sentence[c+1] != ' '){
122+
c++;
123+
}
124+
}
125+
}
126+
else{
127+
if(word.Length > 0){
128+
result.Append(word);
129+
word = new StringBuilder("");
130+
}
131+
result.Append(sentence[c]);
132+
}
133+
}
134+
if(word.Length > 0){
135+
result.Append(word);
136+
}
137+
return result.ToString();
138+
}
139+
}
140+
141+
142+
//Linq
143+
public class Solution {
144+
public string ReplaceWords(IList<string> dictionary, string sentence) {
145+
return string.Join(" ",
146+
sentence.Split()
147+
.Select(w => dictionary
148+
.Where(d => d.Length <= w.Length && w[..d.Length].Equals(d))
149+
.MinBy(x => x.Length) ?? w)
150+
);
151+
}
152+
}

846.IsNStraightHand.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// 846. Hand of Straights
2+
// Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.
3+
4+
// Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.
5+
6+
// Example 1:
7+
// Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
8+
// Output: true
9+
// Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
10+
// Example 2:
11+
// Input: hand = [1,2,3,4,5], groupSize = 4
12+
// Output: false
13+
// Explanation: Alice's hand can not be rearranged into groups of 4.
14+
15+
// Constraints:
16+
// 1 <= hand.length <= 104
17+
// 0 <= hand[i] <= 109
18+
// 1 <= groupSize <= hand.length
19+
20+
public class Solution {
21+
public bool IsNStraightHand(int[] hand, int groupSize) {
22+
if(hand.Length%groupSize != 0)
23+
return false;
24+
25+
Array.Sort(hand);
26+
27+
Dictionary<int,int> cardFreq = new();
28+
foreach(var card in hand){
29+
if(cardFreq.ContainsKey(card))
30+
cardFreq[card]++;
31+
else
32+
cardFreq[card] = 1;
33+
}
34+
while(cardFreq.Any()){
35+
int card = cardFreq.First().Key;
36+
for(int i = 0; i < groupSize; i++){
37+
int currCard = card + i;
38+
if(cardFreq.ContainsKey(currCard)){
39+
cardFreq[currCard]--;
40+
if(cardFreq[currCard] == 0)
41+
cardFreq.Remove(currCard);
42+
}
43+
else
44+
return false;
45+
}
46+
}
47+
return true;
48+
}
49+
}

0 commit comments

Comments
 (0)