tag:blogger.com,1999:blog-5872623838295077986.post1638876415087453477..comments2024-03-20T01:07:56.465-07:00Comments on JavaProblems.com - Free coding problems and exercises: Java > Recursion-1 > countAbc (CodingBat Solution)Unknownnoreply@blogger.comBlogger7125tag:blogger.com,1999:blog-5872623838295077986.post-66634992548330598522021-05-12T06:12:32.791-07:002021-05-12T06:12:32.791-07:00public int countAbc(String str) {
if(str.length(...public int countAbc(String str) {<br /> if(str.length() < 3) return 0;<br /> if(str.startsWith("abc") || str.startsWith("aba")) <br /> return 1 + countAbc(str.substring(2));<br /> return countAbc(str.substring(1));<br />}Anonymoushttps://www.blogger.com/profile/05359114808031764899noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-35530010851950562402021-04-18T12:43:33.607-07:002021-04-18T12:43:33.607-07:00
<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-35117984801659841832021-04-18T12:43:15.241-07:002021-04-18T12:43:15.241-07:00public int countAbc(String str) {
if (str.lengt...public int countAbc(String str) {<br /><br /> if (str.length() < 3) return 0;<br /><br /> if (str.substring(0,3).equals("abc") || str.substring(0,3).equals("aba"))<br /><br /> return 1 + countAbc(str.substring(1));<br /><br /> else<br /><br /> return countAbc(str.substring(1));<br /><br />}<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-53958735794589242502020-09-18T04:10:57.843-07:002020-09-18T04:10:57.843-07:00public int countAbc(String str) {
if(str.isEmpty...public int countAbc(String str) {<br /> if(str.isEmpty()){<br /> return 0;<br /> }<br /> if (str.length() >= 3 <br /> && str.charAt(0) == 'a' <br /> && str.charAt(1) == 'b' <br /> && str.charAt(2) == 'a' <br /> != (str.charAt(2) == 'c')){<br /> return 1 + countAbc(str.substring(2));<br /> }<br /> return countAbc(str.substring(1));<br />}<br />Anonymoushttps://www.blogger.com/profile/18315574734870276996noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-11629048720769332752020-07-22T12:08:40.991-07:002020-07-22T12:08:40.991-07:00public int countAbc(String str) {
if (str.length...public int countAbc(String str) {<br /> if (str.length() < 3)<br /> {<br /> return 0;<br /> }<br /> else<br /> {<br /> if (str.substring(0, 3).equals("abc") || str.substring(0, 3).equals("aba"))<br /> {<br /> return 1 + countAbc(str.substring(2));<br /> }<br /> else<br /> {<br /> return countAbc(str.substring(1));<br /> }<br /> }<br />}<br />Anonymoushttps://www.blogger.com/profile/09746068683979380986noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-9802556922941653872019-12-10T03:44:10.388-08:002019-12-10T03:44:10.388-08:00public int countAbc(String str) {
if (str.length...public int countAbc(String str) {<br /> if (str.length()<3)<br /> return 0;<br /><br /> int count = 0; <br /> if (str.substring(0,2).equals("ab") && (str.charAt(2)=='a' || str.charAt(2)=='c'))<br /> count++;<br /> <br /> return count + countAbc(str.substring(1));<br />}<br />ioannisahttps://www.blogger.com/profile/08926829134923745030noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-87714265889855415322019-04-22T13:42:32.645-07:002019-04-22T13:42:32.645-07:00wouldn't it be more efficient in line 4 to mak...wouldn't it be more efficient in line 4 to make it substring(2) as its impossible for the second place to be the start of another "true" statement.Anonymoushttps://www.blogger.com/profile/17103702030989987665noreply@blogger.com