tag:blogger.com,1999:blog-5872623838295077986.post3517433452060101137..comments2024-03-20T01:07:56.465-07:00Comments on JavaProblems.com - Free coding problems and exercises: Java > Recursion-1 > countHi2 (CodingBat Solution)Unknownnoreply@blogger.comBlogger12125tag:blogger.com,1999:blog-5872623838295077986.post-28289780341920792192022-09-17T10:35:12.512-07:002022-09-17T10:35:12.512-07:00public int countHi2(String str)
{
if(str.length(...public int countHi2(String str) <br />{<br /> if(str.length() < 2)<br /> {<br /> return 0;<br /> }<br /> if(str.startsWith("hi"))<br /> {<br /> return 1+countHi2(str.substring(1));<br /> }<br /> if(str.startsWith("xhi"))<br /> {<br /> return 0+countHi2(str.substring(2));<br /> }<br /> return countHi2(str.substring(1));<br />}Avengerhttps://www.blogger.com/profile/01096178829490949963noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-85227595277647477162022-04-16T10:41:52.631-07:002022-04-16T10:41:52.631-07:00public int countHi2(String str) {
if (str....public int countHi2(String str) {<br /> if (str.length() == 0) {<br /> return 0;<br /> }<br /> if (str.startsWith("hi")) {<br /> return 1 + countHi2(str.substring(1));<br /> }<br /> if (str.startsWith("xhi")) {<br /> return countHi2(str.substring(2));<br /> }<br /> return countHi2(str.substring(1));<br />}<br />Anonymoushttps://www.blogger.com/profile/02049804728265847662noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-58742886459072285542022-02-06T16:30:29.786-08:002022-02-06T16:30:29.786-08:00if(str.equals("hi")) return 1;
if(str.le...if(str.equals("hi")) return 1;<br />if(str.length()<3) return 0;<br />if(str.substring(0,3).equals("xhi")) return countHi2(str.substring(3));<br />if(str.substring(0,2).equals("hi")) return 1 + countHi2(str.substring(2));<br />return countHi2(str.substring(1));Anonymoushttps://www.blogger.com/profile/04449371553951579148noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-57818050517582835322021-12-04T07:38:41.141-08:002021-12-04T07:38:41.141-08:00Simple to understand this I guess.
public int cou...Simple to understand this I guess.<br /><br />public int countHi2(String str) {<br /> if (str.equals("") || str.length()<2) return 0;<br /> if (str.length()>2 && str.substring(0,3).equals("xh")){<br /> return countHi2(str.substring(2));<br /> }<br /> if (str.length()>2 && str.substring(0,3).equals("xhi")){<br /> return countHi2(str.substring(3));<br /> }<br /> else if (str.length()>1 && str.substring(0,2).equals("hi")){<br /> return 1+countHi2(str.substring(2));<br /> }<br /> return countHi2(str.substring(1));<br />}Anonymoushttps://www.blogger.com/profile/03649692210747191324noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-983591627161209422020-09-16T06:35:20.257-07:002020-09-16T06:35:20.257-07:00public int countHi2(String str) {
if (str.length...public int countHi2(String str) {<br /> if (str.length() <= 1){<br /> return 0;<br /> }<br /> if (str.charAt(0) == 'x' && str.charAt(1) == 'h'){<br /> return countHi2(str.substring(2));<br /> }<br /> if (str.charAt(0) == 'h' && str.charAt(1) == 'i'){<br /> return 1 + countHi2(str.substring(2));<br /> }<br /> return countHi2(str.substring(1));<br />}<br />Anonymoushttps://www.blogger.com/profile/18315574734870276996noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-7362064825891914222020-07-27T12:43:39.847-07:002020-07-27T12:43:39.847-07:00public int countHi2(String str) {
if (str.length...public int countHi2(String str) {<br /> if (str.length() < 2)<br /> {<br /> return 0;<br /> }<br /> else<br /> {<br /> if (str.substring(0, 2).equals("hi"))<br /> {<br /> return 1 + countHi2(str.substring(2));<br /> }<br /> else if (str.charAt(0) == 'x' && (3 <= str.length() && str.substring(1, 3).equals("hi")))<br /> {<br /> return countHi2(str.substring(3));<br /> }<br /> else<br /> {<br /> return countHi2(str.substring(1));<br /> }<br /> }<br />}Anonymoushttps://www.blogger.com/profile/09746068683979380986noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-19848309506103839612020-02-27T20:26:38.736-08:002020-02-27T20:26:38.736-08:00if (str.equals("hi")) return 1;
if (st... if (str.equals("hi")) return 1;<br /> if (str.length() < 3) return 0;<br /> <br /> if (str.substring(0,3).equals("xhi")) return countHi2(str.substring(3));<br /> <br /> if (str.substring(0,2).equals("hi")) return 1 + countHi2(str.substring(2));<br /> <br /> return countHi2(str.substring(1));ad ahttps://www.blogger.com/profile/18150446322064954047noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-48526660539698097572020-01-04T06:32:11.630-08:002020-01-04T06:32:11.630-08:00str=str.replaceAll("xhi","");
... str=str.replaceAll("xhi","");<br /> if(str.length() <= 1 ) return 0;<br /> if(str.substring(0,2).equals("hi") ) <br /> return 1 + countHi2(str.substring(1));<br /> else <br /> return countHi2(str.substring(1));<br /> Anonymoushttps://www.blogger.com/profile/04742544701606927089noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-23753083371973871022019-09-22T16:28:33.867-07:002019-09-22T16:28:33.867-07:00I found it easier to use a private helper method
...I found it easier to use a private helper method<br /><br />public int countHi2(String str) {<br /><br />return countHi2Help(str, ' ');<br /> <br />}<br /><br />private int countHi2Help(String str, Character c)<br />{<br /> if(str.length()<=1)<br /> return 0;<br /> <br /> int x = 0;<br /> String sub = str.substring(0, 2);<br /> if(sub.equalsIgnoreCase("hi") && c!='x')<br /> x++;<br /> <br /> return x + countHi2Help(str.substring(1,str.length()), str.charAt(0));<br />}Anonymoushttps://www.blogger.com/profile/06988030709944575665noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-23377501689945920282017-12-07T12:09:38.372-08:002017-12-07T12:09:38.372-08:00cool solution cool solution Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-73484538219925380572015-09-07T05:35:47.420-07:002015-09-07T05:35:47.420-07:00static int countHi2(String str) {
if(str.le... static int countHi2(String str) {<br /> if(str.length()<3)return 0;<br /> int count=0;<br /> if(Pattern.matches("[a-w[y-z]]hi",str.substring(str.length()-3,str.length())))<br /> count=1;<br /> return count+countHi2(str.substring(0,str.length()-1));<br /> <br />}Anonymoushttps://www.blogger.com/profile/17339453228990280965noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-14271854329397633572015-02-15T17:51:26.421-08:002015-02-15T17:51:26.421-08:00here is a simple solution :
public int countHi2(...here is a simple solution : <br /><br />public int countHi2(String str) {<br /> return (str.length() < 2)?0:<br /> (str.substring(0,2).equals("xh"))?<br /> countHi2(str.substring(2)):<br /> (str.substring(0,2).equals("hi"))?<br /> 1 + countHi2(str.substring(1)):<br /> countHi2(str.substring(1)); <br />}Anonymoushttps://www.blogger.com/profile/01743471411364133844noreply@blogger.com