From bc641b72139cd61f7aa84aa3cddc8d2cfbec324d Mon Sep 17 00:00:00 2001 From: fartem Date: Tue, 6 May 2025 07:52:49 +0300 Subject: [PATCH] 2025-05-06 v. 1.0.7: added "3340. Check Balanced String" --- README.md | 1 + lib/easy/3340_check_balanced_string.dart | 18 +++++++++++ pubspec.yaml | 2 +- .../easy/3340_check_balanced_string_test.dart | 30 +++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 lib/easy/3340_check_balanced_string.dart create mode 100644 test/easy/3340_check_balanced_string_test.dart diff --git a/README.md b/README.md index 0c36254..8c0e2e8 100644 --- a/README.md +++ b/README.md @@ -41,5 +41,6 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | | 3536. Maximum Product of Two Digits | [Link](https://leetcode.com/problems/maximum-product-of-two-digits/) | [Link](./lib/easy/3536_maximum_product_of_two_digits.dart) | diff --git a/lib/easy/3340_check_balanced_string.dart b/lib/easy/3340_check_balanced_string.dart new file mode 100644 index 0000000..943f457 --- /dev/null +++ b/lib/easy/3340_check_balanced_string.dart @@ -0,0 +1,18 @@ +class Solution { + bool isBalanced(String num) { + var even = 0; + var odd = 0; + + for (var i = 0; i < num.length; i += 1) { + final curr = int.parse(num[i]); + + if (i.isEven) { + even += curr; + } else { + odd += curr; + } + } + + return even == odd; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index e7c29b0..0801393 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.6 +version: 1.0.7 homepage: https://github.com/fartem/leetcode-dart environment: diff --git a/test/easy/3340_check_balanced_string_test.dart b/test/easy/3340_check_balanced_string_test.dart new file mode 100644 index 0000000..2229fed --- /dev/null +++ b/test/easy/3340_check_balanced_string_test.dart @@ -0,0 +1,30 @@ +import 'package:leetcode_dart/easy/3340_check_balanced_string.dart'; +import 'package:test/test.dart'; + +void main() { + group( + 'Example tests', + () { + final solution = Solution(); + + test( + 'false', + () => expect( + false, + solution.isBalanced( + '1234', + ), + ), + ); + test( + 'true', + () => expect( + true, + solution.isBalanced( + '24123', + ), + ), + ); + }, + ); +}