How to find repeating sequence of characters in a given array?

后端 未结 14 815
故里飘歌
故里飘歌 2020-12-02 12:42

My problem is to find the repeating sequence of characters in the given array. simply, to identify the pattern in which the characters are appearing.

          


        
14条回答
  •  不思量自难忘°
    2020-12-02 13:07

    Using C++:

    //Splits the string into the fragments of given size
    //Returns the set of of splitted strings avaialble
    set split(string s, int frag)
    {
        set uni;
        int len = s.length();
        for(int i = 0; i < len; i+= frag)
        {
            uni.insert(s.substr(i, frag));
        }
    
        return uni;
    }
    
    int main()
    {
    
        string out;
        string s = "carpentercarpenter";
        int len = s.length();
    
          //Optimistic approach..hope there are only 2 repeated strings
          //If that fails, then try to break the strings with lesser number of
          //characters
        for(int i = len/2; i>1;--i)
        {
            set uni = split(s,i);
            if(uni.size() == 1)
            {
                out = *uni.begin();
                break;
            }
        }
    
        cout<

提交回复
热议问题