You are given a string s
of even length. Split this string into two halves of equal lengths, and let a
be the first half and b
be the second half.
Two strings are alike if they have the same number of vowels ('a'
, 'e'
, 'i'
, 'o'
, 'u'
, 'A'
, 'E'
, 'I'
, 'O'
, 'U'
). Notice that s
contains uppercase and lowercase letters.
Return true
if a
and b
are alike. Otherwise, return false
.
Example 1:
Input: s = "book"
Output: true
Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
Example 2:
Input: s = "textbook"
Output: false
Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2. Therefore, they are not alike.
Notice that the vowel o is counted twice.
Constraints:
2 <= s.length <= 1000
s.length
is even.s
consists of uppercase and lowercase letters.
Method 1:
Step 1:
convert the string to character array.
Step 2:
loop the character array by half and another half is in another loop.
For each iteration it call the isVowel() method for checking the character is vowel or not.
For first half if there is a vowel count the vowel for second half is also same.
For example: “book”
first = “bo” second = “ok”
first => count=1 (‘o’)
second => count = 1 (‘o’)
Both are alike.
Initially count=0.
count++ then count — —
count ++ => count=1
count — — => count=0
if(count==0) true both are alike.
CODE:
public class StringHalvesAlike {
public static void main(String[] args) {
String s = "book";
System.out.println("String halves alike: "+halvesAreAlike(s));
}
public static boolean halvesAreAlike(String s)
{
int vowelCount=0;
char[] chArray = s.toCharArray();
for(int i=0; i<chArray.length/2; i++)
if(isVowel(chArray[i])) vowelCount++;
for(int i=chArray.length/2; i<chArray.length; i++)
if(isVowel(chArray[i])) vowelCount--;
return vowelCount==0;
}
public static boolean isVowel(char ch)
{
return ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U';
}
}
output:
String halves alike: true
Solution Link: https://leetcode.com/problems/determine-if-string-halves-are-alike/submissions/1107534501/
Method 2:
Here is the same process of the above but without converting the string to character array.
CODE:
public class StringHalvesAlike {
public static void main(String[] args) {
String s = "book";
System.out.println("String halves alike: "+halvesAreAlike(s));
}
public static boolean halvesAreAlike(String s)
{
int vowelCount=0;
for(int i=0; i<s.length()/2; i++)
if(isVowel(s.charAt(i))) vowelCount++;
for(int i=s.length()/2; i<s.length(); i++)
if(isVowel(s.charAt(i))) vowelCount--;
return vowelCount==0;
}
public static boolean isVowel(char ch)
{
return ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U';
}
}
output:
String halves alike: true
Solution Link: https://leetcode.com/problems/determine-if-string-halves-are-alike/submissions/1107915735/
Method 3:
Step 1:
Consider all the vowels in lower and upper case letters in one string.
Step 2:
Find the half of the string index.
Step 3:
Take the two pointers i=0, j=half.
In every loop check particular character present in the vowel string.
answer = 0
answer++ => answer = 1
answer— — => answer = 0
answer ==0 true.
public class StringHalvesAlike {
public static void main(String[] args) {
String s = "book";
System.out.println("String halves alike: "+halvesAreAlike(s));
}
public static boolean halvesAreAlike(String s) {
String vowels = "aeiouAEIOU";
int half = s.length()/2, ans=0;
for(int i=0, j=half; i<half; i++, j++){
if(vowels.contains(s.charAt(i)+"")){
ans++;
}
if(vowels.contains(s.charAt(j)+"")){
ans--;
}
}
return ans==0;
}
}
String halves alike: true
Solution Link: https://leetcode.com/problems/determine-if-string-halves-are-alike/submissions/1052404858/
Bonus:
How to check character is a vowel or not.
Method 1:
public class Vowel {
public static void main(String[] args) {
char ch ='e';
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U'){
System.out.println("vowel");
}
else{
System.out.println("Not a vowel");
}
}
}
output:
vowel
Method 2:
public class Vowel {
public static void main(String[] args) {
char ch = 'a';
String vowels = "aeiouAEIOU";
if(vowels.contains(ch+"")){
System.out.println("vowel");
}
else{
System.out.println("Not a vowel");
}
}
}
output:
vowel
Method 3:
public class Vowel {
public static void main(String[] args) {
char ch = 's';
String vowels = "aeiouAEIOU";
if(vowels.indexOf(ch) != -1){
System.out.println("vowel");
}
else{
System.out.println("Not a vowel");
}
}
}
output:
Not a vowel
Thank you for reading.
Happy Coding :)