diff --git a/.github/workflows/updateCompletionTable.js b/.github/workflows/updateCompletionTable.js index 8406c81af..cdc7c56e1 100644 --- a/.github/workflows/updateCompletionTable.js +++ b/.github/workflows/updateCompletionTable.js @@ -17,6 +17,7 @@ const FOLDER_TO_LANG = { swift: 'Swift', cpp: 'C++', kotlin: 'Kotlin', + dart: 'Dart', }; const PREPEND_PATH = process.argv[2] || './'; const TEMPLATE_PATH = process.argv[3] || './README_template.md'; diff --git a/dart/0001-two-sum.dart b/dart/0001-two-sum.dart new file mode 100644 index 000000000..084c9c0b1 --- /dev/null +++ b/dart/0001-two-sum.dart @@ -0,0 +1,14 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) + +class Solution { + List twoSum(List nums, int target) { + var map = Map(); + for (int i = 0; i < nums.length; i++) { + var x = target - nums[i]; + if (map.containsKey(x)) return [map[x]!, i]; + map[nums[i]] = i; + } + throw ""; + } +} diff --git a/dart/0036-valid-sudoku.dart b/dart/0036-valid-sudoku.dart new file mode 100644 index 000000000..891f63da0 --- /dev/null +++ b/dart/0036-valid-sudoku.dart @@ -0,0 +1,31 @@ +// As the board size is fixed +// it will result in the following +// Time Complexity: O(1) +// Space Complexity: O(1) + +class Solution { + bool isValidSudoku(List> board) { + var rows = List.filled(9, 0); + var cols = List.filled(9, 0); + var grids = List.filled(9, 0); + + for (int r = 0; r < 9; r++) { + for (int c = 0; c < 9; c++) { + if (board[r][c] == ".") continue; + + var idx = int.parse(board[r][c]) - 1; + + if (rows[r] & 1 << idx != 0) return false; + rows[r] |= 1 << idx; + + if (cols[c] & 1 << idx != 0) return false; + cols[c] |= 1 << idx; + + if (grids[r ~/ 3 * 3 + c ~/ 3] & 1 << idx != 0) return false; + grids[r ~/ 3 * 3 + c ~/ 3] |= 1 << idx; + } + } + + return true; + } +} diff --git a/dart/0049-group-anagrams.dart b/dart/0049-group-anagrams.dart new file mode 100644 index 000000000..90773c3d6 --- /dev/null +++ b/dart/0049-group-anagrams.dart @@ -0,0 +1,23 @@ +// Time Complexity: O(n * m) +// Space Complexity: O(n * m) + +class Solution { + List> groupAnagrams(List strs) { + var map = Map>(); + + for (var str in strs) { + var count = List.filled(26, 0); + for (int i = 0; i < str.length; i++) { + count[str[i].codeUnitAt(0) - 'a'.codeUnitAt(0)]++; + } + var key = count.join("#"); + if (map.containsKey(key)) { + map[key]!.add(str); + continue; + } + map[key] = [str]; + } + + return List.from(map.values); + } +} diff --git a/dart/0217-contains-duplicate.dart b/dart/0217-contains-duplicate.dart new file mode 100644 index 000000000..c05e64bbb --- /dev/null +++ b/dart/0217-contains-duplicate.dart @@ -0,0 +1,15 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) + +class Solution { + bool containsDuplicate(List nums) { + Set hashSet = Set(); + for (int n in nums) { + if (hashSet.contains(n)) { + return true; + } + hashSet.add(n); + } + return false; + } +} diff --git a/dart/0242-valid-anagram.dart b/dart/0242-valid-anagram.dart new file mode 100644 index 000000000..8640c03d5 --- /dev/null +++ b/dart/0242-valid-anagram.dart @@ -0,0 +1,24 @@ +// Time Complexity: O(s + t) +// Space Complexity: O(1) + +class Solution { + bool isAnagram(String s, String t) { + if (s.length != t.length) return false; + + final counter = List.filled(26, 0); + + for (int i = 0; i < s.length; i++) { + counter[s[i].codeUnitAt(0) - 'a'.codeUnitAt(0)]++; + } + + for (int i = 0; i < t.length; i++) { + counter[t[i].codeUnitAt(0) - 'a'.codeUnitAt(0)]--; + } + + for (int val in counter) { + if (val != 0) return false; + } + + return true; + } +} \ No newline at end of file diff --git a/updateSiteData.js b/updateSiteData.js index fa32659c7..9f68d7570 100644 --- a/updateSiteData.js +++ b/updateSiteData.js @@ -71,6 +71,11 @@ const languages = [ directory: 'scala', extension: 'scala' }, + { + name: 'Dart', + directory: 'dart', + extension: 'dart' + }, ] // Rename files to match leetcode url path, and normalize problem number to four digits. diff --git a/verifySiteData.js b/verifySiteData.js index 254f7008c..f360d2332 100644 --- a/verifySiteData.js +++ b/verifySiteData.js @@ -71,6 +71,11 @@ const languageMap = { directory: 'scala', extension: 'scala' }, + dart: { + name: 'Dart', + directory: 'dart', + extension: 'dart' + }, }; const GITHUB_BASE_URL = 'https://github.com/neetcode-gh/leetcode/blob/main';