Solved

countAbc challenge

Posted on 2016-09-21
9
59 Views
Last Modified: 2016-09-22
Hi,


I am working on below challenge.
http://codingbat.com/prob/p161124


I wrote as below and failing some tests.
Expected      Run            
countAbc("abc") → 1      0      X      
countAbc("abcxxabc") → 2      0      X      
countAbc("abaxxaba") → 2      0      X      
countAbc("ababc") → 2      1      X      
countAbc("abxbc") → 0      0      OK      
countAbc("aaabc") → 1      0      X      
countAbc("hello") → 0      0      OK      
countAbc("") → 0      0      OK      
countAbc("ab") → 0      0      OK      
countAbc("aba") → 1      0      X      
countAbc("aca") → 0      0      OK      
countAbc("aaa") → 0      0      OK      
other tests
X      


public int countAbc(String str) {
  
  
 CharSequence cs1="abc";
 CharSequence cs2="aba";
  if(str.length()==0||str.length()==1||str.length()==2){
    return 0;
  }
  if(!str.contains(cs1)||!str.contains(cs2)){
    return 0;
  }
  else return 1+countAbc(str.substring(3));
}

Open in new window


how to fix? any alternate approaches or improvements? please advise
0
Comment
Question by:gudii9
  • 5
  • 3
9 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 41809446
public int countAbc(String str) {
  
  
 CharSequence cs1="abc";
 
 CharSequence cs2="aba";
  if(str.length()==0||str.length()==1||str.length()==2){
    return 0;
  }


  else if(!str.contains(cs1)||!str.contains(cs2)){
    return 0;
  }
  else return 1+countAbc(str.substring(1));
}

Open in new window


above also failing some tests
0
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 41810187
This works:

public int countAbc(String str) {
  String stringABC = "abc";    
  String stringABA = "aba";

  if (str.length()==0||str.length()==1||str.length()==2){
    return 0;
  }

  if(str.startsWith(stringABC)){
    return 1 + countAbc(str.substring(3));
  } else if(str.startsWith(stringABA)){
    return 1 + countAbc(str.substring(2));
  }
  return countAbc(str.substring(1));
}

Open in new window

0
 
LVL 33

Assisted Solution

by:ste5an
ste5an earned 250 total points
ID: 41810195
(str.length()==0||str.length()==1||str.length()==2)

Open in new window


Really?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 37

Expert Comment

by:zzynx
ID: 41810211
No.
if (str.length()<3)

Open in new window

:)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41810797
  else if(!str.contains(cs1)||!str.contains(cs2)){

Open in new window


contains charsequence approach wont work here?
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41810806
public int countAbc(String str) {
  String stringABC = "abc";    
  String stringABA = "aba";

  if (str.length()<3){
    return 0;
  }

  if(str.startsWith(stringABC)){
    return 1 + countAbc(str.substring(3));
  } else if(str.startsWith(stringABA)){
    return 1 + countAbc(str.substring(2));
  }
  return countAbc(str.substring(1));
}

Open in new window


startsWith how is working here why we are moving to str.substring(2));
instead

str.substring(1));
0
 
LVL 7

Author Comment

by:gudii9
ID: 41810811
return 1 + countAbc(str.substring(3));
why one return has 3 in if loop as above and one has 2 as below in else if?

  return 1 + countAbc(str.substring(2));

abc need not be at start right? it could be at middle also?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41810949
If we find the substring "aba", that last 'a' could be the start of another "aba" or "abc" and the challenge wants us to count that. That's why
0
 
LVL 7

Author Comment

by:gudii9
ID: 41811225
got it
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 82
Android studio getdrawable(int) is deprecated 4 81
custom annotations 9 38
varialbe initialization 11 32
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now