2014-09-19 52 views
1

我的代碼所做的是在方法countSubstring中傳遞兩個字符串和一個計數。 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)我們考慮這樣的情況字符串1 = c和字符串2 = AA。 c不包含aa。 如何爲這種情況制定基本案例? 我的嘗試:

問題2)在java中,字符串是如何結束的? 如果我有string1 =「aabbaa」,並且string2 =「aa」。 我從索引0和1得到aa,所以我返回索引0.計算string2.length()+ 0 = 2. 現在我在beginIndex:2到endindex:string2.length-1的子串字符串1獲取新的字符串獲得「bbaa」。 再次搜索,我獲得索引2和3的字符串aa。 如何使字符串aa後的遞歸結束?

回答

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 
+0

能夠擊敗答案我正要給... :) – jamesthollowell 2014-09-19 04:17:33

+0

因爲我通過dianel良章中約20遞歸讀一本書編程在Java中,我得到了遞歸調用,但答案,可以做到這一點更容易 – 2014-09-19 04:23:45

+0

@DavidHang看看這個解決方案是否適合你。如果是的話,接受答案,否則評論。 – Ankush 2014-09-19 05:06:07

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