Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

countPairs challenge

Posted on 2016-09-21
7
Medium Priority
?
107 Views
Last Modified: 2016-09-23
Hi,


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


I am not clear on below description. please advise



Recursion-1 > countPairs
prev  |  next  |  chance
We'll say that a "pair" in a string is two instances of a char separated by a char. So "AxA" the A's make a pair. Pair's can overlap, so "AxAxA" contains 3 pairs -- 2 for A and 1 for x. Recursively compute the number of pairs in the given string.

countPairs("axa") → 1
countPairs("axax") → 2
countPairs("axbx") → 1
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 28

Expert Comment

by:rrz
ID: 41809568
This challenge is very similar to
https://www.experts-exchange.com/questions/28971426/pairstar-challenge.html 
You could use the same approach here.  The code here will very similar.  
In this challenge, you will return a int that will the number of pairs. The pairs in this challenge are separated by another character. So, the conditional is a little different.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41810790
i got meaning now. let me think

so basically same character must be separated by one other character and we need to count such group of 3 counts...

We'll say that a "pair" in a string is two instances of a char separated by a char. So "AxA" the A's make a pair. Pair's can overlap, so "AxAxA" contains 3 pairs -- 2 for A and 1 for x. Recursively compute the number of pairs in the given string.

countPairs("axa") → 1
countPairs("axax") → 2
countPairs("axbx") → 1
0
 
LVL 7

Author Comment

by:gudii9
ID: 41811307
public int countPairs(String str) {
  int count=0;
  

  if(str.length() < 2){
    return 0;
  }
  if(str.charAt(0) == str.charAt(2)){
    return 1+ countPairs(str.substring(1));
  }
  else{
    return  countPairs(str.substring(1));
  }


}

Open in new window


above fails below tests.

Expected      Run            
countPairs("axa") → 1      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:8)      X      
countPairs("axax") → 2      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:8)      X      
countPairs("axbx") → 1      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:8)      X      
countPairs("hi") → 0      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:8)      X      
countPairs("hihih") → 3      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:8)      X      
countPairs("ihihhh") → 3      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:8)      X      
countPairs("ihjxhh") → 0      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:8)      X      
countPairs("") → 0      0      OK      
countPairs("a") → 0      0      OK      
countPairs("aa") → 0      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:8)      X      
countPairs("aaa") → 1      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:8)      X      
other tests
X      
Your progress graph for this problem


please advise
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 7

Author Comment

by:gudii9
ID: 41811310
public int countPairs(String str) {
  int count=0;
  

  if(str.length() < 3){
    return 0;
  }
  if(str.charAt(0) == str.charAt(2)){
    return 1+ countPairs(str.substring(1));
  }
  else{
    return  countPairs(str.substring(1));
  }


}

Open in new window


above passes all tests.
any improvements or alternate approaches?
0
 
LVL 28

Expert Comment

by:rrz
ID: 41811438
My code is exactly the same as your code except for the optional else word. The else is optional because the return statements control the flow.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41811450
if  with if with else
and
if with else if with else

what is difference in this case?
both passes all tests?
0
 
LVL 28

Accepted Solution

by:
rrz earned 2000 total points
ID: 41811827
There is no difference in this case.  
The pseudo code in my mind is

if(condition)return something
if(another condition)return some other thing
return something different

My code;
public int countPairs(String str) {
  if(str.length() < 3)return 0;
  if(str.charAt(0) == str.charAt(2))return 1 + countPairs(str.substring(1));
  return countPairs(str.substring(1));
}

Open in new window

 I don't think the else statement adds anything to the logic. That is why I left them out.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

721 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