Problem:
Given a string, compute recursively the number of times lowercase "hi" appears in the string, however do not count "hi" that have an 'x' immedately before them.
countHi2("ahixhi") → 1
countHi2("ahibhi") → 2
countHi2("xhixhi") → 0
Solution:
public int countHi2(String str) { int len = str.length(); if (len < 2) return 0; if (str.substring(len-2, len).equals("hi")) { if ((len > 2 && str.charAt(len-3) != 'x') || len == 2 ) return 1 + countHi2(str.substring(0, len-1)); } return countHi2(str.substring(0, len-1)); }
here is a simple solution :
ReplyDeletepublic int countHi2(String str) {
return (str.length() < 2)?0:
(str.substring(0,2).equals("xh"))?
countHi2(str.substring(2)):
(str.substring(0,2).equals("hi"))?
1 + countHi2(str.substring(1)):
countHi2(str.substring(1));
}
cool solution
Deletestatic int countHi2(String str) {
ReplyDeleteif(str.length()<3)return 0;
int count=0;
if(Pattern.matches("[a-w[y-z]]hi",str.substring(str.length()-3,str.length())))
count=1;
return count+countHi2(str.substring(0,str.length()-1));
}
I found it easier to use a private helper method
ReplyDeletepublic int countHi2(String str) {
return countHi2Help(str, ' ');
}
private int countHi2Help(String str, Character c)
{
if(str.length()<=1)
return 0;
int x = 0;
String sub = str.substring(0, 2);
if(sub.equalsIgnoreCase("hi") && c!='x')
x++;
return x + countHi2Help(str.substring(1,str.length()), str.charAt(0));
}
str=str.replaceAll("xhi","");
ReplyDeleteif(str.length() <= 1 ) return 0;
if(str.substring(0,2).equals("hi") )
return 1 + countHi2(str.substring(1));
else
return countHi2(str.substring(1));
if (str.equals("hi")) return 1;
ReplyDeleteif (str.length() < 3) return 0;
if (str.substring(0,3).equals("xhi")) return countHi2(str.substring(3));
if (str.substring(0,2).equals("hi")) return 1 + countHi2(str.substring(2));
return countHi2(str.substring(1));
public int countHi2(String str) {
ReplyDeleteif (str.length() < 2)
{
return 0;
}
else
{
if (str.substring(0, 2).equals("hi"))
{
return 1 + countHi2(str.substring(2));
}
else if (str.charAt(0) == 'x' && (3 <= str.length() && str.substring(1, 3).equals("hi")))
{
return countHi2(str.substring(3));
}
else
{
return countHi2(str.substring(1));
}
}
}
public int countHi2(String str) {
ReplyDeleteif (str.length() <= 1){
return 0;
}
if (str.charAt(0) == 'x' && str.charAt(1) == 'h'){
return countHi2(str.substring(2));
}
if (str.charAt(0) == 'h' && str.charAt(1) == 'i'){
return 1 + countHi2(str.substring(2));
}
return countHi2(str.substring(1));
}
Simple to understand this I guess.
ReplyDeletepublic int countHi2(String str) {
if (str.equals("") || str.length()<2) return 0;
if (str.length()>2 && str.substring(0,3).equals("xh")){
return countHi2(str.substring(2));
}
if (str.length()>2 && str.substring(0,3).equals("xhi")){
return countHi2(str.substring(3));
}
else if (str.length()>1 && str.substring(0,2).equals("hi")){
return 1+countHi2(str.substring(2));
}
return countHi2(str.substring(1));
}
if(str.equals("hi")) return 1;
ReplyDeleteif(str.length()<3) return 0;
if(str.substring(0,3).equals("xhi")) return countHi2(str.substring(3));
if(str.substring(0,2).equals("hi")) return 1 + countHi2(str.substring(2));
return countHi2(str.substring(1));
public int countHi2(String str) {
ReplyDeleteif (str.length() == 0) {
return 0;
}
if (str.startsWith("hi")) {
return 1 + countHi2(str.substring(1));
}
if (str.startsWith("xhi")) {
return countHi2(str.substring(2));
}
return countHi2(str.substring(1));
}
public int countHi2(String str)
ReplyDelete{
if(str.length() < 2)
{
return 0;
}
if(str.startsWith("hi"))
{
return 1+countHi2(str.substring(1));
}
if(str.startsWith("xhi"))
{
return 0+countHi2(str.substring(2));
}
return countHi2(str.substring(1));
}