diff --git a/README.md b/README.md index 8c0e2e8..f22445d 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 118. Pascal's Triangle | [Link](https://leetcode.com/problems/pascals-triangle/) | [Link](./lib/easy/118_pascals_triangle.dart) | | 263. Ugly Number | [Link](https://lettcode.com/problems/ugly-number/) | [Link](./lib/easy/263_ugly_number.dart) | | 500. Keyboard Row | [Link](https://leetcode.com/problems/keyboard-row/) | [Link](./lib/easy/500_keyboard_row.dart) | +| 3120. Count the Number of Special Characters I | [Link](https://leetcode.com/problems/count-the-number-of-special-characters-i/) | [Link](./lib/easy/3120_count_the_number_of_special_characters_i.dart) | | 3280. Convert Date to Binary | [Link](https://leetcode.com/problems/convert-date-to-binary/) | [Link](./lib/easy/3280_convert_date_to_binary.dart) | | 3340. Check Balanced String | [Link](https://leetcode.com/problems/check-balanced-string/) | [Link](./lib/easy/3340_check_balanced_string.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/easy/3120_count_the_number_of_special_characters_i.dart b/lib/easy/3120_count_the_number_of_special_characters_i.dart new file mode 100644 index 0000000..f569113 --- /dev/null +++ b/lib/easy/3120_count_the_number_of_special_characters_i.dart @@ -0,0 +1,26 @@ +class Solution { + int numberOfSpecialChars(String word) { + final lower = List.filled(26, 0); + final upper = List.filled(26, 0); + + for (var i = 0; i < word.length; i += 1) { + final char = word[i]; + + if (char.compareTo('a') >= 0 && char.compareTo('z') <= 0) { + lower[char.codeUnitAt(0) - 'a'.codeUnitAt(0)] += 1; + } else { + upper[char.codeUnitAt(0) - 'A'.codeUnitAt(0)] += 1; + } + } + + var result = 0; + + for (var i = 0; i < upper.length; i += 1) { + if (lower[i] > 0 && upper[i] > 0) { + result += 1; + } + } + + return result; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 0801393..c32ba70 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.7 +version: 1.0.8 homepage: https://github.com/fartem/leetcode-dart environment: diff --git a/test/easy/3120_count_the_number_of_special_characters_i_test.dart b/test/easy/3120_count_the_number_of_special_characters_i_test.dart new file mode 100644 index 0000000..1c20688 --- /dev/null +++ b/test/easy/3120_count_the_number_of_special_characters_i_test.dart @@ -0,0 +1,41 @@ +import 'package:leetcode_dart/easy/3120_count_the_number_of_special_characters_i.dart'; +import 'package:test/test.dart'; + +void main() { + group( + 'Example tests', + () { + final solution = Solution(); + + test( + '3', + () => expect( + 3, + solution.numberOfSpecialChars( + 'aaAbcBC', + ), + ), + ); + + test( + '0', + () => expect( + 0, + solution.numberOfSpecialChars( + 'abc', + ), + ), + ); + + test( + '1', + () => expect( + 1, + solution.numberOfSpecialChars( + 'abBCab', + ), + ), + ); + }, + ); +}