Java > Recursion-1 > countHi2 (CodingBat Solution)

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));
  
}


12 comments :

  1. here is a simple solution :

    public 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));
    }

    ReplyDelete
  2. static int countHi2(String str) {
    if(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));

    }

    ReplyDelete
  3. I found it easier to use a private helper method

    public 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));
    }

    ReplyDelete
  4. str=str.replaceAll("xhi","");
    if(str.length() <= 1 ) return 0;
    if(str.substring(0,2).equals("hi") )
    return 1 + countHi2(str.substring(1));
    else
    return countHi2(str.substring(1));

    ReplyDelete
  5. if (str.equals("hi")) return 1;
    if (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));

    ReplyDelete
  6. public int countHi2(String str) {
    if (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));
    }
    }
    }

    ReplyDelete
  7. public int countHi2(String str) {
    if (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));
    }

    ReplyDelete
  8. Simple to understand this I guess.

    public 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));
    }

    ReplyDelete
  9. if(str.equals("hi")) return 1;
    if(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));

    ReplyDelete
  10. public int countHi2(String str) {
    if (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));
    }

    ReplyDelete
  11. public int countHi2(String str)
    {
    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));
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact