-1

Given a string, return true if the string "bat" and "ball" appear the same number of times.

MyApproach

I followed the above approach.I have taken the string "bat" and "ball".I searched in the string whether the pattern "bat" exist or not.I checked each character of the original string and compared with the characters of the bat.Similarly I searched for the pattern ball.It will return true When both bat and ball appear same number of times.

Below is my Code with the Output.

public boolean equal(String str)
{
  String str1="bat";
  String str2="ball";
  int l=str.length();
  int l1=str1.length();
  int l2=str2.length();

  if((l<l1) || (l<l2))
  {
      return false;     
  }
  else
  {
      int m=0;
      int n=0;
      int countbat=0;
      int countball=0;
      int p=0;
      int j=0;
      str=str.toLowerCase();
      str1=str1.toLowerCase();
      str2=str2.toLowerCase();

      while(j<l)
      {
          char c=str.charAt(j);
          char c1=str1.charAt(p);

          if(c==c1){
              p++;

              if(p==l1){
                  countbat++;
                  p=0;
              }    
          }
          else{
              p=0;
          }
          j++;

          } 

          while(m<l)
          {
              char c=str.charAt(m);
              char c2=str1.charAt(n);

              if(c==c2){
                  n++;

                  if(n==l2){
                      countball++;
                      n=0;
                  }    
             }
             else
             {
                 n=0;
             }
             m++;

           } 
           if(countbat==countball)
           return true;
           else
           return false;

    }      
 }

     Parameters         Actual Output   Expected Output

    'bat+ball=cricket'  null            true

I am not able to get the correct output.Can anyone tell me why?

JDurstberger
  • 4,127
  • 8
  • 31
  • 68
Jason arora
  • 550
  • 3
  • 8
  • 22

3 Answers3

1

Your approach is not clear until you explain it briefly. Try this. With this your looping will be very less if you have a big string to search for ball and bat.

    String name = "ball bat ball bat bat ball bat bat";

    int batCount = 0;
    int ballCount = 0;
    int index = 0;
    int startIndex = 0;

    while(index != -1){
        index = name.indexOf("bat", startIndex);
        startIndex = index + 1;
        if(index != -1){
            batCount++;
        }
    }

    index = 0;
    startIndex = 0;

    while(index != -1){
        index = name.indexOf("ball", startIndex);
        startIndex = index + 1;
        if(index != -1){
            ballCount++;
        }
    }

    System.out.println(batCount);  //Outputs 5
    System.out.println(ballCount);  //Outputs 3
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
1

Change char "c2=str1.charAt(n);" to "char c2=str2.charAt(n);" (second while loop)

NedRise
  • 66
  • 6
0

Extract a method to count the occurences of one String in another. Something like,

private static int countWord(String str, String word) {
    int count = 0;
    for (int i = 0; i < str.length() - word.length() + 1; i++) {
        if (str.substring(i, i + word.length()).equals(word)) {
            count++;
        }
    }
    return count;
}

Then you can implement your equal method like

public static boolean equal(String str) {
    return countWord(str, "ball") == countWord(str, "bat");
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249