Substring algorithm

后端 未结 11 1295
小蘑菇
小蘑菇 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 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;
            }
    

提交回复
热议问题