tag:blogger.com,1999:blog-5872623838295077986.post1775534263806637441..comments2024-03-20T01:07:56.465-07:00Comments on JavaProblems.com - Free coding problems and exercises: Java > Recursion-1 > countPairs (CodingBat Solution)Unknownnoreply@blogger.comBlogger5125tag:blogger.com,1999:blog-5872623838295077986.post-32856846451311369382020-09-18T04:15:51.426-07:002020-09-18T04:15:51.426-07:00public int countPairs(String str) {
if (str.leng...public int countPairs(String str) {<br /> if (str.length() <= 2){<br /> return 0; <br /> }<br /> return (str.charAt(0) == str.charAt(2))? <br />1 + countPairs(str.substring(1)) :countPairs(str.substring(1));<br />}<br />Anonymoushttps://www.blogger.com/profile/18315574734870276996noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-12936885207397442742020-09-18T04:11:42.818-07:002020-09-18T04:11:42.818-07:00private int countPairs(String str) {
if(str.char...private int countPairs(String str) {<br /> if(str.charAt(0) == str.charAt(2)){<br /> return 1 + countPairs(str.substring(1));<br /> }<br /> return countPairs(str.substring(1));<br />}<br />Anonymoushttps://www.blogger.com/profile/18315574734870276996noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-41748357256878502712020-01-04T05:24:26.145-08:002020-01-04T05:24:26.145-08:00if ( str.length() <= 2 ) return 0;
if ( str.cha...if ( str.length() <= 2 ) return 0;<br />if ( str.charAt(0) == str.charAt(2) ) return 1 + <br /> countPairs(str.substring(1));<br />else return countPairs(str.substring(1));<br />Anonymoushttps://www.blogger.com/profile/04742544701606927089noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-59491761335038860252019-12-10T03:40:25.300-08:002019-12-10T03:40:25.300-08:00public int countPairs(String str) {
if (str.leng...public int countPairs(String str) {<br /> if (str.length()<3)<br /> return 0;<br /> <br /> int count = (str.charAt(0)==str.charAt(2)) ? 1 : 0;<br /> return count + countPairs(str.substring(1));<br />}ioannisahttps://www.blogger.com/profile/08926829134923745030noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-79034371501874540662019-08-04T17:12:03.186-07:002019-08-04T17:12:03.186-07:00This is my solution :
public int countPairs(Stri...This is my solution :<br /><br /><br />public int countPairs(String str) {<br /> if(str.length()<3) return 0;<br /> <br /> if(str.length()>0 && str.charAt(0)==str.charAt(2)){<br /> <br /> <br /> return 1+ countPairs(str.substring(1));<br /> }<br /> <br /> return countPairs(str.substring(1));<br />}<br />JuanAndreshttps://www.blogger.com/profile/10128999156615250940noreply@blogger.com