Monday, June 27, 2016
codingbat recursion Given a string, compute recursively (no loops) the number of "11" substrings in the string. The "11" substrings should not overlap.
Given a string, compute recursively (no loops) the number of "11" substrings in the string. The "11" substrings should not overlap.
solution
public int count11(String str) {
return count(str,0);
}
int count(String str,int index)
{
if(str.length()-index<=1|| str.length()<2)
return 0;
else if(str.charAt(index)=='1' && str.charAt(index+1)=='1')
{
return 1+count(str,index+2);
}
else
{
return count(str,index+1);
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment