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);
}
}
yos should it count inyooyo?oyoyo? Should that lastyobe counted?