1

I'm doing some recursion exercises and one has quite confused me. The problem stated that I should count the times "yo" has appeared in a string, but if the letter 'o' appears before the "yo", I'm not supposed to count it.

"yoyo" is counted as two, but "yooyo" is counted as one. I have done a code but it doesn't follow the condition of not counting the "yo" that has an 'o' before it. Thanks for the help!

My code:

import java.util.*;

public class Mp3
{
    static int oui(String arr, int index)
    {
      int count = 0;
      if(index >= arr.length())
        return 0;
  
      if(count == 0)
      {
        if(arr.charAt(index) == 'y')
        {
          if(arr.charAt(index + 1) == 'o')
            count++;
          else if(arr.charAt(index - 1) == 'o' && arr.charAt(index - 2) != 'y')
            --count;
          else
            return count + oui(arr, index + 1);
        }
     }
     return count + oui(arr, index + 1);
  }

  public static void main (String[] args)
  {
     String inp3 = "yoyooyoxhadjiohioyooyoyoxxyoyo";

     int res3 = oui(inp3, 0);

     System.out.println(inp3 + ":" + res3);
  }
}
4
  • What do you mean by "but 'yooyo' is not accepted"? Your method is not supposed to accept or reject strings, is it? It is supposed to count things. How many yos should it count in yooyo? Commented May 25, 2021 at 2:55
  • What about oyoyo? Should that last yo be counted? Commented May 25, 2021 at 2:58
  • It should according to his question @Sweeper Commented May 25, 2021 at 2:59
  • the "yo" counted in "yooyo" is only one (the first yo is the only one counted. Commented May 25, 2021 at 3:05

2 Answers 2

3
import java.util.*;

public class Mp3
{
  static int oui(String arr, int index)
  {
    int count = 0;
    if(index >= arr.length())
      return 0;
    if(arr.charAt(index)=='o' && arr.charAt(index+1)=='y'&&arr.charAt(index+2)=='o') 
      
       return count + oui(arr, index + 3);
      

    if(arr.charAt(index) == 'y' && arr.charAt(index + 1) == 'o')
       
       return count+ 1 +oui(arr, index + 2);
     
    
    return count + oui(arr, index + 1);
 }


  public static void main (String[] args)
 {
  String inp3 = "yoyooyoxhadjiohioyooyoyoxxyoyo";

  int res3 = oui(inp3, 0);

  System.out.println(inp3 + ":" + res3);
 }

}

**I tried not to change a lot from your code :

  1. I check if index= o then if followed by yo I skip them and check afterward
  2. I check if index= y and followed by o then I take them and check afterwards cause I always check for o first
  3. Simple :) If you have any questions post a comment and I will try to explain better**
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for your explanation! I understand them better now. Thanks again!
@PoohSan You're more than welcome I'm glad I helped :)!
0

Make this simple:

Algo

let's assume index represents 0-th index initially.

  1. check the index-ed and index+1-ed character , if it is y and o increment count by +1 and index-ed value by +2;
  2. if it is not, simply ignore the count and increment index-ed value by +1.
  3. do it repetitively until you reach at the end of the string.

Code

caller :: getCountYo("yoyooyoxhadjiohioyooyoyoxxyoyo", 0);

private int getCountYo(String input, int index) {
    int count = 0;
    /* when you reach at the end of the string */
    if(index == input.length() || index+1 == input.length()) {
        return 0;
    }else {
        /* when "yo" found as a sub-string */
        if(input.charAt(index) == 'y' && input.charAt(index+1) == 'o') {
            count += getCountYo(input, index+2)+1;
        }else if(input.charAt(index) != 'y') {
            count += getCountYo(input, index+1);
        }
    }
    return count;
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.