6315. Count the Number of Vowel Strings in Range
- Difficulty: Easy
You are given a 0-indexed array of string words and two integers left and right.
A string is called a vowel string if it starts with a vowel character and ends with a vowel character where vowel characters are 'a', 'e', 'i', 'o', and 'u'.
Return the number of vowel strings words[i] where i belongs to the inclusive range [left, right].
Example 1:
Input: words = ["are","amy","u"], left = 0, right = 2
Output: 2
Explanation:
- "are" is a vowel string because it starts with 'a' and ends with 'e'.
- "amy" is not a vowel string because it does not end with a vowel.
- "u" is a vowel string because it starts with 'u' and ends with 'u'.
The number of vowel strings in the mentioned range is 2.
Example 2:
Input: words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4 Output: 3 Explanation: - "aeo" is a vowel string because it starts with 'a' and ends with 'o'. - "mu" is not a vowel string because it does not start with a vowel. - "ooo" is a vowel string because it starts with 'o' and ends with 'o'. - "artro" is a vowel string because it starts with 'a' and ends with 'o'. The number of vowel strings in the mentioned range is 3.
Constraints:
1 <= words.length <= 10001 <= words[i].length <= 10words[i]consists of only lowercase English letters.0 <= left <= right < words.length
Solution:
class Solution {
public:
int vowelStrings(vector<string>& words, int left, int right) {
int count=0;
for(int i=left; i<=right; i++)
{
if((words[i][0] == 97 || words[i][0] == 101 || words[i][0] == 105 || words[i][0] == 111 || words[i][0] == 117) && (words[i][words[i].size()-1] == 97 || words[i][words[i].size()-1] == 101 || words[i][words[i].size()-1] == 105 || words[i][words[i].size()-1] == 111 || words[i][words[i].size()-1] == 117))
{
count++;
}
}
return count;
}
};
6316. Rearrange Array to Maximize Prefix Score
- Difficulty: Medium
You are given a 0-indexed integer array nums. You can rearrange the elements of nums to any order (including the given order).
Let prefix be the array containing the prefix sums of nums after rearranging it. In other words, prefix[i] is the sum of the elements from 0 to i in nums after rearranging it. The score of nums is the number of positive integers in the array prefix.
Return the maximum score you can achieve.
Example 1:
Input: nums = [2,-1,0,1,-3,3,-3]
Output: 6
Explanation: We can rearrange the array into nums = [2,3,1,-1,-3,0,-3].
prefix = [2,5,6,5,2,2,-1], so the score is 6.
It can be shown that 6 is the maximum score we can obtain.
Example 2:
Input: nums = [-2,-3,0]
Output: 0
Explanation: Any rearrangement of the array will result in a score of 0.
Constraints:
1 <= nums.length <= 105-106 <= nums[i] <= 106
Solution:
class Solution {
public:
int maxScore(vector<int>& nums) {
int n = nums.size();
int count = 0;
vector<long> prefix(nums.size());
sort(nums.begin(), nums.end(), greater<long long>());
for(int i=0; i<nums.size(); i++)
{
if(i == 0)
{
prefix[i] = nums[i];
}
else
{
prefix[i] = prefix[i-1] + nums[i];
}
}
for(int i=0; i<prefix.size(); i++)
{
if(prefix[i] > 0)
{
count++;
}
}
return count;
}
};
2588. Count the Number of Beautiful Subarrays
- Difficult: Medium
You are given a 0-indexed integer array nums. In one operation, you can:
- Choose two different indices
iandjsuch that0 <= i, j < nums.length. - Choose a non-negative integer
ksuch that thekthbit (0-indexed) in the binary representation ofnums[i]andnums[j]is1. - Subtract
2kfromnums[i]andnums[j].
A subarray is beautiful if it is possible to make all of its elements equal to 0 after applying the above operation any number of times.
Return the number of beautiful subarrays in the array nums.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [4,3,1,2,4]
Output: 2
Explanation: There are 2 beautiful subarrays in nums: [4,3,1,2,4] and [4,3,1,2,4].
- We can make all elements in the subarray [3,1,2] equal to 0 in the following way:
- Choose [3, 1, 2] and k = 1. Subtract 21 from both numbers. The subarray becomes [1, 1, 0].
- Choose [1, 1, 0] and k = 0. Subtract 20 from both numbers. The subarray becomes [0, 0, 0].
- We can make all elements in the subarray [4,3,1,2,4] equal to 0 in the following way:
- Choose [4, 3, 1, 2, 4] and k = 2. Subtract 22 from both numbers. The subarray becomes [0, 3, 1, 2, 0].
- Choose [0, 3, 1, 2, 0] and k = 0. Subtract 20 from both numbers. The subarray becomes [0, 2, 0, 2, 0].
- Choose [0, 2, 0, 2, 0] and k = 1. Subtract 21 from both numbers. The subarray becomes [0, 0, 0, 0, 0].
Example 2:
Input: nums = [1,10,4]
Output: 0
Explanation: There are no beautiful subarrays in nums.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 106
Solution
class Solution {
public:
long long beautifulSubarrays(vector<int>& nums) {
map<int, int> prefix;
long long ans = 0, x = 0;
prefix[0] = 1;
for(int i=0; i<nums.size(); i++)
{
x = x^nums[i];
if(prefix.find(x) != prefix.end())
{
ans = ans + prefix[x];
}
prefix[x] = prefix[x] + 1;
}
return ans;
}
};

Leave a comment