Substring algorithm

2019-03-22 14:58发布

Can someone explain to me how to solve the substring problem iteratively?

The problem: given two strings S=S1S2S3Sn and T=T1T2T3Tm, with m is less than or equal to n, determine if T is a substring of S.

11条回答
叼着烟拽天下
2楼-- · 2019-03-22 15:03
if (T == string.Empty) return true;
for (int i = 0; i <= S.Length - T.Length; i++) {
    for (int j = 0; j < T.Length; j++) {
        if (S[i + j] == T[j]) {
            if (j == (T.Length - 1)) return true;
        }
        else break;
    }
}
return false;
查看更多
男人必须洒脱
3楼-- · 2019-03-22 15:04
// runs in best case O(n) where no match, worst case O(n2) where strings match

var s = "hippopotumus"
var t = "tum"

for(var i=0;i<s.length;i++)
    if(s[i]==t[0])
        for(var ii=i,iii=0; iii<t.length && i<s.length; ii++, iii++){
            if(s[ii]!=t[iii]) break
            else if (iii==t.length-1) console.log("yay found it at index: "+i)
        }
查看更多
劳资没心,怎么记你
4楼-- · 2019-03-22 15:04

Though its pretty old post, I am trying to answer it. Kindly correct me if anything is wrong,

package com.amaze.substring;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CheckSubstring {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Please enter the main string");
    String mainStr = br.readLine();

    System.out.println("Enter the substring that has to be searched");
    String subStr = br.readLine();

    char[] mainArr = new char[mainStr.length()];
    mainArr = mainStr.toCharArray();
    char[] subArr = new char[subStr.length()];
    subArr = subStr.toCharArray();
    boolean tracing = false;
    //System.out.println("Length of substring is "+subArr.length);
    int j = 0;

    for(int i=0; i<mainStr.length();i++){

        if(!tracing){
            if(mainArr[i] == subArr[j]){
                tracing = true;
                j++;
            }
        } else {
            if (mainArr[i] == subArr[j]){
                //System.out.println(mainArr[i]);
                //System.out.println(subArr[j]);
                j++;
                System.out.println("Value of j is "+j);
                if((j == subArr.length)){
                    System.out.println("SubString found");
                    return;
                }
            } else {
                j=0;
                tracing = false;
            }
        }
    }

    System.out.println("Substring not found");

}

}
查看更多
不美不萌又怎样
5楼-- · 2019-03-22 15:06

It would go something like this:

m==0? return true
cs=0
ct=0
loop
    cs>n-m? break
    char at cs+ct in S==char at ct in T?
    yes:
        ct=ct+1
        ct==m? return true
    no:
        ct=0
        cs=cs+1

end loop
return false
查看更多
叼着烟拽天下
6楼-- · 2019-03-22 15:07

A naive algorithm would be to test at each position 0 < in-m of S if Si+1Si+2Si+m=T1T2Tm. For n=7 and m=5:

i=0:  S1S2S3S4S5S6S7
      | | | | |
      T1T2T3T4T5

i=1:  S1S2S3S4S5S6S7
        | | | | |
        T1T2T3T4T5

i=2:  S1S2S3S4S5S6S7
          | | | | |
          T1T2T3T4T5

The algorithm in pseudo-code:

// we just need to test if n ≤ m 
IF n > m:
    // for each offset on that T can start to be substring of S
    FOR i FROM 0 TO n-m:
        // compare every character of T with the corresponding character in S plus the offset
        FOR j FROM 1 TO m:
            // if characters are equal
            IF S[i+j] == T[j]:
                // if we’re at the end of T, T is a substring of S
                IF j == m:
                    RETURN true;
                ENDIF;
            ELSE:
                BREAK;
            ENDIF;
        ENDFOR;
    ENDFOR;
ENDIF;
RETURN false;
查看更多
Emotional °昔
7楼-- · 2019-03-22 15:09

Is a O(n*m) algorithm, where n and m are the size of each string. In C# it would be something similar to:

   public static bool IsSubtring(char[] strBigger, char[] strSmall)
        {
            int startBigger = 0;
            while (startBigger <= strBigger.Length - strSmall.Length)
            {
                int i = startBigger, j = 0;

                while (j < strSmall.Length && strSmall[j] == strBigger[i])
                {
                    i++;
                    j++;
                }

                if (j == strSmall.Length)
                    return true;
                startBigger++;
            }

            return false;
        }
查看更多
登录 后发表回答