Skip to content

Commit 16448c5

Browse files
authored
Merge pull request #1857 from NikSWE/main
Add first dart solution
2 parents d278f0f + 5a6c5c3 commit 16448c5

8 files changed

+118
-0
lines changed

.github/workflows/updateCompletionTable.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const FOLDER_TO_LANG = {
1717
swift: 'Swift',
1818
cpp: 'C++',
1919
kotlin: 'Kotlin',
20+
dart: 'Dart',
2021
};
2122
const PREPEND_PATH = process.argv[2] || './';
2223
const TEMPLATE_PATH = process.argv[3] || './README_template.md';

dart/0001-two-sum.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
class Solution {
5+
List<int> twoSum(List<int> nums, int target) {
6+
var map = Map<int, int>();
7+
for (int i = 0; i < nums.length; i++) {
8+
var x = target - nums[i];
9+
if (map.containsKey(x)) return [map[x]!, i];
10+
map[nums[i]] = i;
11+
}
12+
throw "";
13+
}
14+
}

dart/0036-valid-sudoku.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// As the board size is fixed
2+
// it will result in the following
3+
// Time Complexity: O(1)
4+
// Space Complexity: O(1)
5+
6+
class Solution {
7+
bool isValidSudoku(List<List<String>> board) {
8+
var rows = List.filled(9, 0);
9+
var cols = List.filled(9, 0);
10+
var grids = List.filled(9, 0);
11+
12+
for (int r = 0; r < 9; r++) {
13+
for (int c = 0; c < 9; c++) {
14+
if (board[r][c] == ".") continue;
15+
16+
var idx = int.parse(board[r][c]) - 1;
17+
18+
if (rows[r] & 1 << idx != 0) return false;
19+
rows[r] |= 1 << idx;
20+
21+
if (cols[c] & 1 << idx != 0) return false;
22+
cols[c] |= 1 << idx;
23+
24+
if (grids[r ~/ 3 * 3 + c ~/ 3] & 1 << idx != 0) return false;
25+
grids[r ~/ 3 * 3 + c ~/ 3] |= 1 << idx;
26+
}
27+
}
28+
29+
return true;
30+
}
31+
}

dart/0049-group-anagrams.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time Complexity: O(n * m)
2+
// Space Complexity: O(n * m)
3+
4+
class Solution {
5+
List<List<String>> groupAnagrams(List<String> strs) {
6+
var map = Map<String, List<String>>();
7+
8+
for (var str in strs) {
9+
var count = List.filled(26, 0);
10+
for (int i = 0; i < str.length; i++) {
11+
count[str[i].codeUnitAt(0) - 'a'.codeUnitAt(0)]++;
12+
}
13+
var key = count.join("#");
14+
if (map.containsKey(key)) {
15+
map[key]!.add(str);
16+
continue;
17+
}
18+
map[key] = [str];
19+
}
20+
21+
return List.from(map.values);
22+
}
23+
}

dart/0217-contains-duplicate.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
class Solution {
5+
bool containsDuplicate(List<int> nums) {
6+
Set<int> hashSet = Set<int>();
7+
for (int n in nums) {
8+
if (hashSet.contains(n)) {
9+
return true;
10+
}
11+
hashSet.add(n);
12+
}
13+
return false;
14+
}
15+
}

dart/0242-valid-anagram.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time Complexity: O(s + t)
2+
// Space Complexity: O(1)
3+
4+
class Solution {
5+
bool isAnagram(String s, String t) {
6+
if (s.length != t.length) return false;
7+
8+
final counter = List.filled(26, 0);
9+
10+
for (int i = 0; i < s.length; i++) {
11+
counter[s[i].codeUnitAt(0) - 'a'.codeUnitAt(0)]++;
12+
}
13+
14+
for (int i = 0; i < t.length; i++) {
15+
counter[t[i].codeUnitAt(0) - 'a'.codeUnitAt(0)]--;
16+
}
17+
18+
for (int val in counter) {
19+
if (val != 0) return false;
20+
}
21+
22+
return true;
23+
}
24+
}

updateSiteData.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ const languages = [
7171
directory: 'scala',
7272
extension: 'scala'
7373
},
74+
{
75+
name: 'Dart',
76+
directory: 'dart',
77+
extension: 'dart'
78+
},
7479
]
7580

7681
// Rename files to match leetcode url path, and normalize problem number to four digits.

verifySiteData.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ const languageMap = {
7171
directory: 'scala',
7272
extension: 'scala'
7373
},
74+
dart: {
75+
name: 'Dart',
76+
directory: 'dart',
77+
extension: 'dart'
78+
},
7479
};
7580

7681
const GITHUB_BASE_URL = 'https://github.com/neetcode-gh/leetcode/blob/main';

0 commit comments

Comments
 (0)