how to solve this without loops at all

is there a way to solve this without any loops at all? (no for loops)
No description
9 Replies
JavaBot
JavaBot4w ago
This post has been reserved for your question.
Hey @SidKid! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
ENDERFUN
ENDERFUN4w ago
You can call the count11 with let's say substrings and when it starts with "11" you return 1 + count11(11) or count11() otherwise And when substring is of length 1 you terminate recursion by returning 0
LinusHuck
LinusHuck4w ago
Sure something like this should work
public static int count(String str, String toCount)
{
if (str.length() < toCount.length())
{
return 0;
}
if (str.startsWith(toCount))
{
return 1 + count(str.substring(toCount.length()), toCount);
}
return count(str.substring(1), toCount);
}
public static int count(String str, String toCount)
{
if (str.length() < toCount.length())
{
return 0;
}
if (str.startsWith(toCount))
{
return 1 + count(str.substring(toCount.length()), toCount);
}
return count(str.substring(1), toCount);
}
Should work ig /run
public class A{
public static void main(String[] args){
System.out.println(count("11abc11"));
System.out.println(count("abc11x11x11"));
System.out.println(count("111"));
}

public static int count(String str)
{
if (str.length() < 2)
{
return 0;
}
if (str.startsWith("11"))
{
return 1 + count(str.substring(2));
}
return count(str.substring(1));
}

}
public class A{
public static void main(String[] args){
System.out.println(count("11abc11"));
System.out.println(count("abc11x11x11"));
System.out.println(count("111"));
}

public static int count(String str)
{
if (str.length() < 2)
{
return 0;
}
if (str.startsWith("11"))
{
return 1 + count(str.substring(2));
}
return count(str.substring(1));
}

}
I Run Code
I Run Code4w ago
Here is your java(15.0.2) output @LinusHuck
2
3
1
2
3
1
ENDERFUN
ENDERFUN4w ago
@LinusHuck in the assignment it looks like count can only take one parameter It'd be something like this:
public static int count11(String str) {
if (str.length() < 2) return 0;
if (str.startsWith("11")) return 1 + count11(str.substring(1));
return count(str.substring(1));
}
public static int count11(String str) {
if (str.length() < 2) return 0;
if (str.startsWith("11")) return 1 + count11(str.substring(1));
return count(str.substring(1));
}
dan1st
dan1st4w ago
+ count11(str.substring(2)) if you want 111 to yield 1 instead of 2
ENDERFUN
ENDERFUN4w ago
Yea that's right, I haven't noticed the last sentence
SidKid
SidKidOP4w ago
do you see what could be the problem with this attempt at the question?
public int count11(String str) {
//what happens when all 1s
if(str.length() < 2){ //cannot possibly have 11 which is 2 chracters long if string less than 2
return 0;
}
//going through string using substrings (passing substrings into the recursive function)

if(str.substring(0,1) == "11"){
if(str.length() == 2){
return count11(str.substring(1)) + 1;
}
else if (str.substring(2,2) == "1") {
return count11(str.substring(1));
}
else {
return count11(str.substring(1))+1;
}
}
else{
return count11(str.substring(1));
}
}
public int count11(String str) {
//what happens when all 1s
if(str.length() < 2){ //cannot possibly have 11 which is 2 chracters long if string less than 2
return 0;
}
//going through string using substrings (passing substrings into the recursive function)

if(str.substring(0,1) == "11"){
if(str.length() == 2){
return count11(str.substring(1)) + 1;
}
else if (str.substring(2,2) == "1") {
return count11(str.substring(1));
}
else {
return count11(str.substring(1))+1;
}
}
else{
return count11(str.substring(1));
}
}
JavaBot
JavaBot4w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?