簡體   English   中英

在不使用contains或indexOf方法的情況下檢查給定的字符串是否包含子字符串

[英]To check if a given string contains a substring without using contains or indexOf method

在下面的代碼中,我面臨的問題是如何使內部循環每次從給定索引而不是0開始。

邏輯:

substr = rohan; 
mainstr = herohanda

代碼首先與mainstr每個字符一起檢查substr的第一個字符'r' ,直到找到匹配mainstr為止。 當找到匹配項時,程序返回到外循環並遞增i以檢查substr的第二個substr (即o )是否匹配該字符的下一個字符(找到與'r'匹配的索引旁邊的字符)。 mainStr 問題是如何從下一個字符開始內部循環。 這是每次都從初始索引(0)開始。

碼:

public class SubstringInString {
   public static void isSubstring(String subStr, String mainStr){
         int flag = 0;
         int counter = 0;
        OUTER: for(int i = flag; i<subStr.length(); i = i+flag){
            INNER:  for(int j = 0; j< mainStr.length(); j=counter ){
                        if(subStr.charAt(i) == mainStr.charAt(j)){
                            counter++;
                            flag++;

                            continue OUTER;
                        }
                        else
                        {
                            if((mainStr.length() - i) >= subStr.length()){
                                counter ++;
                                flag = 0;

                                continue INNER;
                            }

                            else{
                                System.out.println("Main String does not contain the substring");
                            }
                        }
                    }
                }

            //  System.out.println("Match found at " + j-subStr.length());
        }
}

請告訴我如何解決此問題,以及是否有更好的方法可以解決此問題。 提前致謝

從技術上講,這解決了以下問題:

public static void isSubstring(String subStr, String mainStr){
    return mainStr.matches(".*\\Q" + subStr + "\\E.*");
}

假設我們要確定s2是否為s1的子字符串。

有兩種基本情況:

  1. s2長度為0:在這種情況下,我們可以返回true
  2. s2長度大於s1長度:我們可以返回false

通用方法如下:遍歷s1每個字符,看看它是否與s2的第一個字符匹配。 如果是,則檢查s1其余字符是否與s2其余字符匹配。 如果是這樣,則返回true 如果完全搜索了s1 ,但沒有找到匹配項,則返回false

這是在Java中的樣子:

static boolean isSubstring(String s1, String s2){
    int n1 = s1.length();
    int n2 = s2.length();

    if(n2 == 0)     // Base Case 1
        return true;

    if(n2 > n1)    // Base Case 2
        return false;

    for(int i = 0; i < s1.length(); i++)
        if(s1.charAt(i) == s2.charAt(0))        // if first char matches
            if(isRestMatch(i+1, s1, s2))       // check if rest match
                return true;

    return false;
}

static boolean isRestMatch(int start, String s1, String s2){
    int n = s2.length();
    for(int i = start, x = 1; i < n; i++, x++)
        if(s1.charAt(i) != s2.charAt(x))
            return false;

    return true;
}

我找到了這個Java實現

public String strStr(String haystack, String needle) {

    int needleLen = needle.length();
    int haystackLen = haystack.length();

    if (needleLen == haystackLen && needleLen == 0)
        return "";

    if (needleLen == 0)
        return haystack;

    for (int i = 0; i < haystackLen; i++) {
        // make sure in boundary of needle
        if (haystackLen - i + 1 < needleLen)
            return null;

        int k = i;
        int j = 0;

        while (j < needleLen && k < haystackLen && needle.charAt(j) == haystack.charAt(k)) {
            j++;
            k++;
            if (j == needleLen)
                return haystack.substring(i);
        }

    }

    return null;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM