gudii9
asked on
countPairs challenge
Hi,
I am working on below challenge.
http://codingbat.com/prob/p154048
I am not clear on below description. please advise
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
ASKER
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...
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
ASKER
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));
}
}
above fails below tests.
Expected Run
countPairs("axa") → 1 Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 2 (line number:8) X
countPairs("axax") → 2 Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 2 (line number:8) X
countPairs("axbx") → 1 Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 2 (line number:8) X
countPairs("hi") → 0 Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 2 (line number:8) X
countPairs("hihih") → 3 Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 2 (line number:8) X
countPairs("ihihhh") → 3 Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 2 (line number:8) X
countPairs("ihjxhh") → 0 Exception:java.lang.StringIndexOutOf BoundsExce ption: 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.StringIndexOutOf BoundsExce ption: String index out of range: 2 (line number:8) X
countPairs("aaa") → 1 Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 2 (line number:8) X
other tests
X
Your progress graph for this problem
please advise
ASKER
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));
}
}
above passes all tests.
any improvements or alternate approaches?
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.
ASKER
if with if with else
and
if with else if with else
what is difference in this case?
both passes all tests?
and
if with else if with else
what is difference in this case?
both passes all tests?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.