2

I am trying to solve above problem. Below is my solution. Appreciate feedback/comments.

public static boolean areMoreVowels(String str, int c, int v){
    String vowels = "aeiou";

    if(vowels.indexOf(str.charAt(0))>=0){
        v++;
    }else{
        c++;
    }

    if(str.length()==1){
        return v > c;   
    }

    return areMoreVowels(str.substring(1), c, v);

}

public static void main(String[] args) {
    System.out.println(areMoreVowels("aeiouttrrrrr", 0, 0));
}
intvprep
  • 71
  • 8
  • Do you have a question? Is this not working? This isn't really a "code review" site. – smead Mar 04 '17 at 06:17
  • I don't like the way I call the function first time. My question is, is there a better way to solve this ? – intvprep Mar 04 '17 at 06:21
  • I don't understand why you would use recursion to do this. A simple for loop should be easy enough. – smead Mar 04 '17 at 06:23
  • @smead - because the problem says so.. – intvprep Mar 04 '17 at 06:26
  • Well then it's a stupid problem. There are way better problems to learn about recursion with. I wouldn't waste your time trying to perfect it, if what you wrote works :) – smead Mar 04 '17 at 06:27
  • @intvprep Please read: [What should I do when someone answers my question](http://stackoverflow.com/help/someone-answers) – Rumid Mar 17 '17 at 14:24

3 Answers3

1

I think your code it's ok! But if you look for other way, another idea could be

Int areMoreVowels(string str, int pos)
    if  pos == str.length()
        return 0
    k = areMoreVowels(str, pos+1)
    if str[pos] == Vowel()
        return k + 1
    return k - 1

main()
    if areMoreVowels("aeiouttrrrrr", 0) > 0 
        print "Yes"
    else
        print "No"
EmmanuelAC
  • 141
  • 3
1

Personally I'd split this up into two separate problems for simplicity:

  • counting vowels
  • verifying whether the number of vowels is greater than that of vowels

Something along these lines:

count_vowels(str, p):
    if p == len(str) return 0

    return count_vowels(str, p + 1) + (is_vowel(str[p]) ? 1 : 0)

more_vowels(str):
    return count_vowels(str, 0) > (len(str) / 2)

Assigning a single, simple purpose to each method is generally a better approach than creating a conditional structure to squeeze separate functionality into a single method.

1

Your approach is fine. I don't know if you are aware of it or not, but you used tail recursion which really matters in some languages (like Scala), and can reduce chance for stack overflow. Also your method already return your answer, what I would consider as an advantage. You can consider reducing amount of variables:

public static boolean areMoreVowels(String str, int v){
  String vowels = "aeiou"; //do you consider 'y' as a vovel?

  if (vowels.indexOf(str.charAt(0)) >= 0) {
    v++;
  } else {
    v--;
  }

  if (str.length() == 1) {
    return v > 0;
  }

  return areMoreVowels(str.substring(1), v);
}

public static void main(String[] args) {
    System.out.println(areMoreVowels("aeiouttrrrrr", 0, 0));
}
Community
  • 1
  • 1
Rumid
  • 1,627
  • 2
  • 21
  • 39