Problem:
Return the number of times that the string "hi" appears anywhere in the given string.
countHi("abc hi ho") → 1
countHi("ABChi hi") → 2
countHi("hihi") → 2
Solution:
public int countHi(String str) { int count =0; if (str.length() ==1 && str.charAt(0) == 'h') count = 0; else { for(int i = 0;i<str.length();i++) { if ( (str.charAt(i) == 'h') && (str.charAt(i+1) == 'i') ) count+=1; } } return count; }
public int countHi(String str) {
ReplyDeleteint count = 0;
for(int i=0; i<str.length()-1; i++){
if(str.charAt(i) == 'h' && str.charAt(i+1) == 'i')
count++;
}
return count;
}
int count = 0;
ReplyDeletefor (int i = 0; i < str.length() - 1; i++){
if (str.charAt(i) == 'h' && str.charAt(i + 1)== 'i')
count += 1;
}
return count;
public int countHi(String str) {
ReplyDeleteint count = 0;
for (int i=0; i<str.length(); i++){
if (str.substring(i).startsWith("hi")) count++;
}return count;
}
public int countHi(String str) {
ReplyDeleteString found = "hi";
int full = str.length();
str = str.replace(found, "");
int cut = str.length();
return (full - cut) / found.length();
}
public int countHi(String str) {
ReplyDeleteif(str.isEmpty()|| str.length() <= 1){
return 0;
}
if(str == "hi"){
return 1;
}
else
{
int L =str.length();
if(str.charAt(L-2) == 'h' && str.charAt(L-1) == 'i'){
return 1 + countHi(str.substring(0,L-2));
}
else
{
return countHi(str.substring(0,L-1));
}
}
}
public int countHi(String str) {
ReplyDeleteint count=0;
for(int i=0;i<str.length()-1;i++){
if((str.charAt(i)=='h')&&(str.charAt(i+1)=='i'))count++;
}
return count;
}
//IMDAD MOMIN
ReplyDeletepublic int countHi(String str) {
int count=0;
for(int i=0 ; i<str.length()-1 ;i++) {
if(str.substring(i,i+2).equals("hi")) count++;
}
return count;
}
public int countHi(String word) {
ReplyDeleteif(word.isEmpty()){
return 0;
}
if(word.startsWith("hi")){
return 1 + countHi(word.substring(2));
}
return countHi(word.substring(1));
}
public int countHi(String word) {
ReplyDeleteif(word.isEmpty()){
return 0;
}
return (word.startsWith("hi"))? 1 + countHi(word.substring(2)): countHi(word.substring(1));
}
public int countHi(String str) {
ReplyDeleteint c=0;
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='h'&&str.charAt(i+1)=='i')
c++;
}
return c;
}