【问题标题】:Given a string, return true if the string "bat" and "ball" appear the same number of times [duplicate]给定一个字符串,如果字符串“bat”和“ball”出现的次数相同,则返回true [重复]
【发布时间】:2016-02-28 19:19:31
【问题描述】:

给定一个字符串,如果字符串“bat”和“ball”出现的次数相同,则返回true。

我的方法

我按照上面的方法。我取了字符串“bat”和“ball”。我在字符串中搜索模式“bat”是否存在。我检查了原始字符串的每个字符并与字符进行比较蝙蝠的。类似地,我搜索了图案球。它会返回 true 当球棒和球出现的次数相同时。

下面是我的输出代码。

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

我无法获得正确的输出。谁能告诉我 为什么?

【问题讨论】:

标签: java string


【解决方案1】:

改变字符“c2=str1.charAt(n);”到“char c2=str2.charAt(n);” (第二个while循环)

【讨论】:

  • 感谢 Ned Rise 这是我的愚蠢错误。
  • 没问题,不客气:)
【解决方案2】:

在您简要解释之前,您的方法并不清楚。尝试这个。有了这个,如果你有一个大字符串来搜索球和球棒,你的循环将非常少。

    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

【讨论】:

  • 你试过这个吗?此解决方案将需要最少的循环次数..
  • 我会留作参考。
【解决方案3】:

提取一种方法来计算一个String 在另一个中的出现次数。类似的,

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

然后你可以实现你的equal 方法

public static boolean equal(String str) {
    return countWord(str, "ball") == countWord(str, "bat");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2022-12-04
    • 2021-12-25
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多