Skip to content

Commit

Permalink
Sync LeetCode submission - Determine if String Halves Are Alike (cpp)
Browse files Browse the repository at this point in the history
  • Loading branch information
pradeeptosarkar committed Jan 12, 2024
1 parent 67f84c4 commit 7e2b2e2
Showing 1 changed file with 10 additions and 42 deletions.
52 changes: 10 additions & 42 deletions LeetCode/problems/determine_if_string_halves_are_alike/solution.cpp
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;
}
};

0 comments on commit 7e2b2e2

Please sign in to comment.