This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1056 from NouraAlgohary/main
add day 5
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...n Length/05- Maximum Number of Vowels in a Substring of Given Length (Noura Algohary).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Author: Noura Algohary | ||
// Use sliding window | ||
class Solution { | ||
public: | ||
int maxVowels(string s, int k) { | ||
int n = s.size(), vowelsCnt = 0; | ||
|
||
for(int i = 0; i < k ; i++) | ||
{ | ||
if(s[i] == 'a' || s[i] == 'e' ||s[i] == 'i' ||s[i] == 'o' ||s[i] == 'u') | ||
vowelsCnt++; | ||
|
||
} | ||
int maxVowels = vowelsCnt; | ||
for(int i = k; i < n; i++) | ||
{ | ||
if(s[i] == 'a' || s[i] == 'e' ||s[i] == 'i' ||s[i] == 'o' ||s[i] == 'u') | ||
++vowelsCnt; | ||
|
||
if(s[i - k] == 'a' || s[i - k] == 'e' ||s[i - k] == 'i' ||s[i - k] == 'o' ||s[i - k] == 'u') | ||
--vowelsCnt; | ||
|
||
if(vowelsCnt > maxVowels) | ||
maxVowels = vowelsCnt; | ||
} | ||
return maxVowels; | ||
} | ||
}; |
24 changes: 24 additions & 0 deletions
24
...en Length/05- Maximum Number of Vowels in a Substring of Given Length (Noura Algohary).py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Author: Noura Algohary | ||
# Use sliding window | ||
|
||
class Solution: | ||
def maxVowels(self, s: str, k: int) -> int: | ||
vowelsCnt = 0 | ||
|
||
for i in range(k): | ||
if s[i] in ['a', 'e', 'i', 'o', 'u']: | ||
vowelsCnt += 1 | ||
|
||
maxVowels = vowelsCnt | ||
|
||
for i in range(k, len(s)): | ||
if s[i] in ['a', 'e', 'i', 'o', 'u']: | ||
vowelsCnt += 1 | ||
|
||
if s[i - k] in ['a', 'e', 'i', 'o', 'u']: | ||
vowelsCnt -= 1 | ||
|
||
if vowelsCnt > maxVowels: | ||
maxVowels = vowelsCnt | ||
|
||
return maxVowels |