Substring algorithm

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

    I know I'm late to the game but here is my version of it (in C#):

        bool isSubString(string subString, string supraString)
        {
            for (int x = 0; x <= supraString.Length; x++)
            {
                int counter = 0;
                if (subString[0] == supraString[x]) //find initial match
                {
                    for (int y = 0; y <= subString.Length; y++)
                    {
                        if (subString[y] == supraString[y+x])
                        {
                            counter++;
                            if (counter == subString.Length)
                            {
                                return true;
                            }
                        } 
                    }
                }
            }
            return false;
        }
    

提交回复
热议问题