结束String,递归和搜索事件

时间:2014-09-19 04:02:08

标签: java string recursion

我的代码所做的是在方法countSubstring中传递两个字符串和一个count。 countSubString计算strOne中strTwo的出现次数。 但我很难,因为我不了解一些事情:

    public class CountingSubString
    {
        int CountSubString(String strOne, String strTwo, int count)
        {
          int i = 0;
          int foundAtIndex = strOne.indexOf(strTwo, i);
           if(foundAtIndex == -1)
        {
            i++;
        }
         else//(foundAtIndex != -1)
        {
         count++;
          int newStartIndex = foundAtIndex + strTwo.length();
         String StringFromString = strOne.substring(newStartIndex, strOne.length()-1);
          count = count + countSubString(StringFromString, strTwo, count);
         return count;
        }
         return count;
       }
         public class TestCountingSubString 
      {
           public static void main(String[] argv)
         {
           String s2 = new String("abab");
            String s3 = new String("ab");
          String s4 = new String("aabbaa");
          String s5 = new String("aa");
          countingSubString CountOfString = new countingSubString();
          int count = CountOfString.countSubString(s2, s3, 0);
         System.out.println(count);
          }
      }  

问题1)考虑一下string1 = c,string2 = aa的情况。 aa不包含在c中。  如何为此案例制作基础案例? 我的尝试:

问题2)在java中字符串如何结束? 如果我有string1 =" aabbaa",和string2 =" aa"。 我从索引0和1得到aa,所以我返回索引0. compute string2.length()+ 0 = 2。 现在我将beginIndex:2中的字符串1子串到endindex:string2.length-1以获取新字符串以获取" bbaa"。 再次搜索,我在索引2和3处获得字符串aa。 如何在字符串aa之后使我的递归结束?

2 个答案:

答案 0 :(得分:2)

为什么让事情变得复杂。它的java,使用它的功能。

String string1 = "abab";
Pattern p = Pattern.compile("ab");
Matcher m = p.matcher(string1);
int count = 0;
while (m.find()){
    count +=1;
}
System.out.println(count);

同样为了您的理解,子字符串函数具有以下格式

public String substring(int beginIndex, int endIndex)

其中

beginIndex -- the begin index, inclusive.

endIndex -- the end index, exclusive.

问题1中提出的安全条件

if (strOne == null || strOne.equals("") || strTwo.length() < sub.length())
 return 0;

问题2的解决方案

int index =  strOne.indexOf(strTwo);
if(index!=-1){
    count++;
    count+= countSubString(strOne.substring(index+1),strTwo,0);
}

完整的解决方案是

class countingSubString
 {
    int countSubString(String strOne, String strTwo, int count)
    {
      if (strOne == null || strOne.equals("") || strOne.length() < strTwo.length())
        return 0;

      int index =  strOne.indexOf(strTwo);
      if(index!=-1){
        count++;
        count+= countSubString(strOne.substring(index+1),strTwo,0);
      }

      return count;
   }
}

另外remove public modifier from class countingSubString因为一个文件中只能有一个公共类。并遵循命名约定,因此类名应为

CountingSubString instead of countingSubString

答案 1 :(得分:1)

您可以使用递归函数,如下所示。我稍微修改了类和函数名。

您不需要将'count'参数传递给countSub函数,因为它最终将以递归方式返回。

public class Count 
{
  public static void main(String[] argv) {
    String s2 = new String("ababab");
    String s3 = new String("ab");

    String s4 = new String("aabbaa");
    String s5 = new String("aa");


    int count = countSub(s2, s3);
    System.out.println(count);
  }

  public static int countSub(String strOne, String strTwo) {

    int foundAtIndex = strOne.indexOf(strTwo);

    if(foundAtIndex == -1) {
      return 0;

    } else {
      int newStartIndex = foundAtIndex + strTwo.length();
      String newString = strOne.substring(newStartIndex, strOne.length());

      return (1 + countSub(newString, strTwo));
    }
  }
}