Substring algorithm

后端 未结 11 1293
小蘑菇
小蘑菇 2021-02-11 03:38

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

The problem: given two strings S=S1S2S

相关标签:
11条回答
  • 2021-02-11 03:58

    Here's a list of string searching algorithms

    Depending on your needs, a different algorithm may be a better fit, but Boyer-Moore is a popular choice.

    0 讨论(0)
  • 2021-02-11 04:00

    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;
            }
    
    0 讨论(0)
  • 2021-02-11 04:05
    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;
    
    0 讨论(0)
  • 2021-02-11 04: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;
    
    0 讨论(0)
  • 2021-02-11 04:08

    Not sure what language you're working in, but here's an example in C#. It's a roughly n2 algorithm, but it will get the job done.

    bool IsSubstring (string s, string t)
    {
       for (int i = 0; i <= (s.Length - t.Length); i++)
       {
          bool found = true;
    
          for (int j = 0; found && j < t.Length; j++)
          {
             if (s[i + j] != t[j])
                 found = false;
          }
    
          if (found)
             return true;
       }
    
       return false;
    }
    0 讨论(0)
提交回复
热议问题