Contents

Babble 1 (with.Java)

   May 19, 2024     2 min read

This is an article about the “Babbling 1 (with.Java)” problem.

As I solve coding test problems, I look back on the problems I solved and look into different solution methods to get to know myself.

Let’s look at the problem first.

problem

Museok is taking care of his 6-month-old nephew.

My nephew can only pronounce the four sounds “aya”, “ye”, “woo”, and “ma” using them at most once.

When the string array babbling is given as a parameter, complete the solution function so that it returns the number of words that the babbling nephew can pronounce.

Restrictions

  • 1 ≤ length of babbling ≤ 100
  • 1 ≤ length of babbling[i] ≤ 15
  • In each string of babbling, “aya”, “ye”, “woo”, and “ma” each appear at most once.
  • That is, among all possible substrings of each string, “aya”, “ye”, “woo”, and “ma” occur only once.
  • The string consists of only lowercase alphabet letters.

Input/Output Example

babblingresult
[“aya”, “yee”, “u”, “maa”, “wyeoo”]1
[“ayaye”, “uuuma”, “ye”, “yemawoo”, “ayaa”]3

My solution to the problem

class Solution {
 public int solution(String[] babbling) {
 int answer = 0;
 String[] vaildBabblings = {"aya", "ye", "woo", "ma"};

 for(String sound: babbling){
 for(String validBabbling : validBabblings){
 sound = sound.replace(validBabbling," ");
 }

 sound = sound.replace(" ","");

 if(sound.equals("")){
 answer++;
 }
 }
 return answer;
 }
}

Solution review

First, an array vaildBabblings is declared, which stores valid syllable combinations.

Use a double loop to remove valid syllable combinations from each string (sound).

The outer loop repeats the input string array (babbling), and the inner loop repeats valid syllable combinations.

Replaces each valid syllable combination with a space in the string.

After removing all valid syllable combinations, we remove spaces from the string.

Increment answer only if the string with all spaces removed is an empty string (“”).

Finally, it returns an answer.

This code provides a simple way to find and count valid syllable combinations in an input string.