-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission - Determine if String Halves Are Alike (cpp)
- Loading branch information
1 parent
67f84c4
commit 7e2b2e2
Showing
1 changed file
with
10 additions
and
42 deletions.
There are no files selected for viewing
52 changes: 10 additions & 42 deletions
52
LeetCode/problems/determine_if_string_halves_are_alike/solution.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 |
---|---|---|
@@ -1,46 +1,14 @@ | ||
class Solution { | ||
public: | ||
bool halvesAreAlike(string s) { | ||
|
||
string vowels = "aeiouAEIOU"; | ||
int n = s.length(); | ||
|
||
int alikeOne = 0, alikeTwo = 0; | ||
|
||
//cout statements are for debugging purposes on a cpp compiler | ||
string halfOne = s.substr(0, (n/2)); | ||
//cout << (n/2) - 1<< endl; | ||
//cout << halfOne << endl; | ||
string halfTwo = s.substr(n/2, n-1); | ||
//cout << halfTwo << endl; | ||
|
||
for(char ch : vowels) | ||
{ | ||
for(char c: halfOne) | ||
{ | ||
if(ch == c) | ||
{ | ||
alikeOne++; | ||
} | ||
} | ||
for(char c: halfTwo) | ||
{ | ||
if(ch == c) | ||
{ | ||
alikeTwo++; | ||
} | ||
} | ||
} | ||
|
||
//cout << alikeOne << endl << alikeTwo << endl; | ||
|
||
if(alikeOne == alikeTwo) | ||
{ | ||
return true; | ||
} | ||
|
||
|
||
return false; | ||
|
||
bool halvesAreAlike(string s) | ||
{ | ||
int ans=0; | ||
int n=s.size(); | ||
|
||
for(int i=0;i<n;i++) | ||
if(s[i]=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u' or s[i]=='A' or s[i]=='E' or s[i]=='I' or s[i]=='O' or s[i]=='U') | ||
(i<n/2)?ans+=1:ans-=1; | ||
|
||
return !ans; | ||
} | ||
}; |