diff --git a/.gitignore b/.gitignore index c002574..cd48cd9 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ build/ .idea/ + +.cursor diff --git a/README.md b/README.md index 48ecbca..35a49c7 100644 --- a/README.md +++ b/README.md @@ -38,5 +38,6 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 111. Minimum Depth of Binary Tree | [Link](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Link](./lib/easy/111_minimum_depth_of_binary_tree.dart) | | 112. Path Sum | [Link](https://leetcode.com/problems/path-sum/) | [Link](./lib/easy/112_path_sum.dart) | | 118. Pascal's Triangle | [Link](https://leetcode.com/problems/pascals-triangle/) | [Link](./lib/easy/118_pascals_triangle.dart) | +| 500. Keyboard Row | [Link](https://leetcode.com/problems/keyboard-row/) | [Link](./lib/easy/500_keyboard_row.dart) | | 3280. Convert Date to Binary | [Link](https://leetcode.com/problems/convert-date-to-binary/) | [Link](./lib/easy/3280_convert_date_to_binary.dart) | | 3516. Find Closest Person | [Link](https://leetcode.com/problems/find-closest-person/) | [Link](./lib/easy/3516_find_closest_person.dart) | diff --git a/lib/common/list_node.dart b/lib/common/list_node.dart index 97fcdb8..0cccded 100644 --- a/lib/common/list_node.dart +++ b/lib/common/list_node.dart @@ -2,5 +2,8 @@ class ListNode { int val; ListNode? next; - ListNode([this.val = 0, this.next]); + ListNode([ + this.val = 0, + this.next, + ]); } diff --git a/lib/common/tree_node.dart b/lib/common/tree_node.dart index 4b566b2..174466e 100644 --- a/lib/common/tree_node.dart +++ b/lib/common/tree_node.dart @@ -3,5 +3,9 @@ class TreeNode { TreeNode? left; TreeNode? right; - TreeNode([this.val = 0, this.left, this.right]); + TreeNode([ + this.val = 0, + this.left, + this.right, + ]); } diff --git a/lib/easy/500_keyboard_row.dart b/lib/easy/500_keyboard_row.dart new file mode 100644 index 0000000..8f5411b --- /dev/null +++ b/lib/easy/500_keyboard_row.dart @@ -0,0 +1,40 @@ +class Solution { + List findWords(List words) { + const firstRow = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'}; + const secondRow = {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'}; + const thirdRow = {'z', 'x', 'c', 'v', 'b', 'n', 'm'}; + + final result = []; + + for (final word in words) { + final chars = word.toLowerCase().split(''); + var row = -1; + + if (firstRow.contains(chars[0])) { + row = 1; + } else if (secondRow.contains(chars[0])) { + row = 2; + } else if (thirdRow.contains(chars[0])) { + row = 3; + } + + var isValid = true; + for (var i = 1; i < chars.length; i++) { + final char = chars[i]; + if ((row == 1 && !firstRow.contains(char)) || + (row == 2 && !secondRow.contains(char)) || + (row == 3 && !thirdRow.contains(char))) { + isValid = false; + + break; + } + } + + if (isValid) { + result.add(word); + } + } + + return result; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index aff6720..250ce18 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: leetcode_dart description: Some solved problems from https://leetcode.com on Dart -version: 1.0.3 +version: 1.0.4 homepage: https://github.com/fartem/leetcode-dart environment: diff --git a/test/easy/500_keyboard_row_test.dart b/test/easy/500_keyboard_row_test.dart new file mode 100644 index 0000000..a2e4fa3 --- /dev/null +++ b/test/easy/500_keyboard_row_test.dart @@ -0,0 +1,57 @@ +import 'package:leetcode_dart/easy/500_keyboard_row.dart'; +import 'package:test/test.dart'; + +void main() { + group( + 'Example tests', + () { + final solution = Solution(); + + test( + 'test case 1', + () => expect( + [ + 'Alaska', + 'Dad', + ], + solution.findWords( + [ + 'Hello', + 'Alaska', + 'Dad', + 'Peace', + ], + ), + ), + ); + + test( + 'test case 2', + () => expect( + [], + solution.findWords( + [ + 'omk', + ], + ), + ), + ); + + test( + 'test case 3', + () => expect( + [ + 'adsdf', + 'sfd', + ], + solution.findWords( + [ + 'adsdf', + 'sfd', + ], + ), + ), + ); + }, + ); +}