From bf94aff668c673c6982cd3de4df9d08eaf6c172f Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Sat, 17 Dec 2022 01:43:25 +0400 Subject: [PATCH] feat: add Determine if String Halves Are Alike LeetCode (#1168) * add leetcode Determine if String Halves Are Alike * fix variable name Co-authored-by: David Leal --- leetcode/DIRECTORY.md | 1 + leetcode/src/1704.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 leetcode/src/1704.c diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index c0cf88f9..f45ca7a6 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -106,6 +106,7 @@ | 1769 | [Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/) | [C](./src/1769.c) | Medium | | 1524 | [Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/) | [C](./src/1524.c) | Medium | | 1653 | [Minimum Deletions to Make String Balanced](https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/) | [C](./src/1653.c) | Medium | +| 1704 | [Determine if String Halves Are Alike](Determine if String Halves Are Alike) | [C](./src/1704.c) | Easy | | 1752 | [Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [C](./src/1752.c) | Easy | | 2024 | [Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | [C](./src/2024.c) | Medium | | 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium | diff --git a/leetcode/src/1704.c b/leetcode/src/1704.c new file mode 100644 index 00000000..25251983 --- /dev/null +++ b/leetcode/src/1704.c @@ -0,0 +1,38 @@ +bool isVowel(char chr){ + switch(chr){ + case 'a': + case 'e': + case 'i': + case 'o': + case 'u': + case 'A': + case 'E': + case 'I': + case 'O': + case 'U': + return true; + } + + return false; +} + +// Counting +// Runtime: O(n) +// Space: O(1) +bool halvesAreAlike(char * s){ + int lenS = strlen(s); + int halfVowels = 0; + int currVowels = 0; + + for (int i = 0; i < lenS; i++){ + if (isVowel(s[i])){ + currVowels++; + } + + if (2 * (i + 1) == lenS){ + halfVowels = currVowels; + } + } + + return 2 * halfVowels == currVowels; +}