HackerRank : Week of Code 31 – Beautiful Word

By | April 10, 2017
Beautiful Word : Hackerrank

We consider a word, , to be beautiful if the following two conditions are satisfied:

  • No two consecutive characters are the same.
  • No two consecutive characters are in the following vowel set: a, e, i, o, u, y. Note that we consider y to be a vowel in this challenge.

For example:

image

The string batman is beautiful because it satisfies the given criteria; however, apple has two consecutive occurrences of the same letter (pp) and beauty has three consecutive vowels (eau), so those words are not beautiful.

Given , print Yes if it is beautiful or No if it is not.

Input Format
A single string denoting .
Constraints

  • consists of lowercase English alphabetic letters only (i.e., a through z).

Output Format
Print Yes if is beautiful, or No if it is not.
Sample Input 0

abacaba

Sample Output 0

Yes

Explanation 0
Every pair of consecutive characters consists of one vowel and one consonant, so the word is beautiful and we print Yes.
Sample Input 1

badd

Sample Output 1

No

Explanation 1
There are two consecutive occurrences of d, so it is not beautiful and we print No.
Sample Input 2

yes

Sample Output 2

No

Explanation 2
The first pair of letters (y and e) both appear in our set of vowel characters, so the word is not beautiful and we print No.
Solution

Solution is pretty easy. 
Hint: replace vowels.

package weekOfCode31;
import java.util.Scanner;
public class BeautifulWord {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		String str = scan.nextLine();
		char strArr[] = str.toCharArray();

		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'
					|| str.charAt(i) == 'u' || str.charAt(i) == 'y') {
				strArr[i] = '#';
			}
		}
		int same = 0;
		for (int j = 0; j < str.length() - 1; j++) {
			if (strArr[j] == strArr[j + 1]) {
				same = 1;
				break;
			}
		}
		if (same == 1)
			System.out.println("No");
		else
			System.out.println("Yes");

	}
}