Skip to content

Commit 1ea373b

Browse files
committed
feat: updates jan 11 2025
1 parent f447ff9 commit 1ea373b

9 files changed

+321
-0
lines changed

1009-complement-base-10.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
int findComplement(int num) {
3+
if (num == 0) return 1;
4+
5+
int mask = num;
6+
mask |= mask >> 1;
7+
mask |= mask >> 2;
8+
mask |= mask >> 4;
9+
mask |= mask >> 8;
10+
mask |= mask >> 16;
11+
12+
return num ^ mask;
13+
}
14+
}
15+
16+
void main() {
17+
final solution = Solution();
18+
19+
// Test Case 1
20+
final num1 = 5;
21+
print('Example 1: ${solution.findComplement(num1)}'); // Output: 2
22+
23+
// Test Case 2
24+
final num2 = 1;
25+
print('Example 2: ${solution.findComplement(num2)}'); // Output: 0
26+
}

1400-k-palindrome-strings.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
bool canConstruct(String s, int k) {
3+
if (k > s.length) return false;
4+
if (k == s.length) return true;
5+
6+
List<int> freq = List.filled(26, 0);
7+
for (int i = 0; i < s.length; i++) {
8+
freq[s.codeUnitAt(i) - 'a'.codeUnitAt(0)]++;
9+
}
10+
11+
int oddCount = 0;
12+
for (int count in freq) {
13+
if (count % 2 == 1) {
14+
oddCount++;
15+
}
16+
}
17+
return oddCount <= k;
18+
}
19+
}
20+
21+
void main() {
22+
final solution = Solution();
23+
24+
print('Test 1: ${solution.canConstruct("annabelle", 2)}'); // true
25+
print('Test 2: ${solution.canConstruct("leetcode", 3)}'); // false
26+
print('Test 3: ${solution.canConstruct("true", 4)}'); // true
27+
28+
}

145-binary-tree-postorder.dart

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Definition for a binary tree node.
3+
*/
4+
class TreeNode {
5+
int val;
6+
TreeNode? left;
7+
TreeNode? right;
8+
TreeNode([this.val = 0, this.left, this.right]);
9+
}
10+
11+
class Solution {
12+
List<int> postorderTraversal(TreeNode? root) {
13+
List<int> result = [];
14+
if (root == null) return result;
15+
16+
List<TreeNode> stack1 = [];
17+
List<TreeNode> stack2 = [];
18+
19+
stack1.add(root);
20+
21+
while (stack1.isNotEmpty) {
22+
TreeNode node = stack1.removeLast();
23+
stack2.add(node);
24+
25+
if (node.left != null) {
26+
stack1.add(node.left!);
27+
}
28+
if (node.right != null) {
29+
stack1.add(node.right!);
30+
}
31+
}
32+
33+
while (stack2.isNotEmpty) {
34+
result.add(stack2.removeLast().val);
35+
}
36+
37+
return result;
38+
}
39+
}
40+
41+
void main() {
42+
Solution solution = Solution();
43+
44+
TreeNode? createTree(List<int?> values) {
45+
if (values.isEmpty || values[0] == null) return null;
46+
47+
TreeNode root = TreeNode(values[0]!);
48+
List<TreeNode> queue = [root];
49+
int i = 1;
50+
51+
while (queue.isNotEmpty && i < values.length) {
52+
TreeNode current = queue.removeAt(0);
53+
54+
if (i < values.length && values[i] != null) {
55+
current.left = TreeNode(values[i]!);
56+
queue.add(current.left!);
57+
}
58+
i++;
59+
60+
if (i < values.length && values[i] != null) {
61+
current.right = TreeNode(values[i]!);
62+
queue.add(current.right!);
63+
}
64+
i++;
65+
}
66+
67+
return root;
68+
}
69+
70+
var tree1 = createTree([1, null, 2, 3]);
71+
print('Test 1: ${solution.postorderTraversal(tree1)}'); // [3,2,1]
72+
73+
var tree2 = createTree([1]);
74+
print('Test 2: ${solution.postorderTraversal(tree2)}'); // [1]
75+
}

2116-validate-locked-parentheses.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
bool canBeValid(String s, String locked) {
3+
if (s.length % 2 != 0) return false;
4+
5+
int balance = 0;
6+
int wild = 0;
7+
8+
for (int i = 0; i < s.length; i++) {
9+
if (locked[i] == '1') {
10+
balance += s[i] == '(' ? 1 : -1;
11+
} else {
12+
wild++;
13+
}
14+
15+
if (balance + wild < 0) return false;
16+
}
17+
18+
balance = 0;
19+
wild = 0;
20+
21+
for (int i = s.length - 1; i >= 0; i--) {
22+
if (locked[i] == '1') {
23+
balance += s[i] == ')' ? 1 : -1;
24+
} else {
25+
wild++;
26+
}
27+
28+
if (balance + wild < 0) return false;
29+
}
30+
31+
return true;
32+
}
33+
}
34+
35+
void main() {
36+
final solution = Solution();
37+
38+
print(solution.canBeValid("))()))", "010100")); // true
39+
print(solution.canBeValid("()()", "0000")); // true
40+
print(solution.canBeValid(")", "0")); // false
41+
}

2185-counting-words-prefix.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
int prefixCount(List<String> words, String pref) {
3+
int count = 0;
4+
5+
for (String word in words) {
6+
if (word.startsWith(pref)) {
7+
count++;
8+
}
9+
}
10+
11+
return count;
12+
}
13+
}
14+
15+
void main() {
16+
final solution = Solution();
17+
18+
final words1 = ["pay", "attention", "practice", "attend"];
19+
final pref1 = "at";
20+
print('1: ${solution.prefixCount(words1, pref1)}'); // Output: 2
21+
22+
final words2 = ["leetcode", "win", "loops", "success"];
23+
final pref2 = "code";
24+
print('2: ${solution.prefixCount(words2, pref2)}'); // Output: 0
25+
26+
final words3 = ["abc", "acd", "abd", "abx", "bcd"];
27+
final pref3 = "ab";
28+
print('3: ${solution.prefixCount(words3, pref3)}'); // Output: 3
29+
}

476-number-complement.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
int findComplement(int num) {
3+
if (num == 0) return 1;
4+
5+
int mask = num;
6+
mask |= mask >> 1;
7+
mask |= mask >> 2;
8+
mask |= mask >> 4;
9+
mask |= mask >> 8;
10+
mask |= mask >> 16;
11+
12+
return num ^ mask;
13+
}
14+
}
15+
16+
void main() {
17+
final solution = Solution();
18+
19+
// Test Case 1
20+
final num1 = 5;
21+
print('Example 1: ${solution.findComplement(num1)}'); // Output: 2
22+
23+
// Test Case 2
24+
final num2 = 1;
25+
print('Example 2: ${solution.findComplement(num2)}'); // Output: 0
26+
}

590-nary-tree-postorder.dart

Whitespace-only changes.

860-lemonade-change.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
bool lemonadeChange(List<int> bills) {
3+
int five = 0;
4+
int ten = 0;
5+
6+
for (int bill in bills) {
7+
if (bill == 5) {
8+
five++;
9+
} else if (bill == 10) {
10+
if (five == 0) return false;
11+
five--;
12+
ten++;
13+
} else {
14+
// bill == 20
15+
if (ten > 0 && five > 0) {
16+
ten--;
17+
five--;
18+
} else if (five >= 3) {
19+
five -= 3;
20+
} else {
21+
return false;
22+
}
23+
}
24+
}
25+
26+
return true;
27+
}
28+
}
29+
30+
void main() {
31+
final solution = Solution();
32+
33+
final bills1 = [5, 5, 5, 10, 20];
34+
print('Example 1: ${solution.lemonadeChange(bills1)}'); // Output: true
35+
36+
final bills2 = [5, 5, 10, 10, 20];
37+
print('Example 2: ${solution.lemonadeChange(bills2)}'); // Output: false
38+
}

916-word-subsets.dart

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Solution {
2+
List<String> wordSubsets(List<String> words1, List<String> words2) {
3+
List<String> result = [];
4+
List<int> maxFreq = List.filled(26, 0);
5+
6+
for (String word in words2) {
7+
List<int> freq = getFrequency(word);
8+
for (int i = 0; i < 26; i++) {
9+
maxFreq[i] = maxFreq[i] > freq[i] ? maxFreq[i] : freq[i];
10+
}
11+
}
12+
13+
for (String word in words1) {
14+
List<int> freq = getFrequency(word);
15+
bool isUniversal = true;
16+
17+
for (int i = 0; i < 26; i++) {
18+
if (freq[i] < maxFreq[i]) {
19+
isUniversal = false;
20+
break;
21+
}
22+
}
23+
24+
if (isUniversal) {
25+
result.add(word);
26+
}
27+
}
28+
29+
return result;
30+
}
31+
32+
List<int> getFrequency(String word) {
33+
List<int> freq = List.filled(26, 0);
34+
for (int i = 0; i < word.length; i++) {
35+
freq[word.codeUnitAt(i) - 'a'.codeUnitAt(0)]++;
36+
}
37+
return freq;
38+
}
39+
}
40+
41+
void main() {
42+
final solution = Solution();
43+
44+
List<String> words1 = ["amazon", "apple", "facebook", "google", "leetcode"];
45+
List<String> words2 = ["e", "o"];
46+
print('Test 1: ${solution.wordSubsets(words1, words2)}');
47+
// Output: ["facebook","google","leetcode"]
48+
49+
List<String> words3 = ["amazon", "apple", "facebook", "google", "leetcode"];
50+
List<String> words4 = ["l", "e"];
51+
print('Test 2: ${solution.wordSubsets(words3, words4)}');
52+
// Output: ["apple","google","leetcode"]
53+
54+
List<String> words5 = ["warrior", "world", "win"];
55+
List<String> words6 = ["wr"];
56+
print('Test 3: ${solution.wordSubsets(words5, words6)}');
57+
// Output: ["warrior","world"]
58+
}

0 commit comments

Comments
 (0)