c++ simple solution


  • 0
    M

    class Solution {
    public:
    int repeatedStringMatch(string A, string B) {
    int La = A.size(), Lb = B.size() ;
    for(int i=0; i<La; i++)
    {
    if(matched(A, B, i)) return (i+Lb)/La + ( ((i+Lb)%La >0 )?1:0 ) ;
    }
    return -1 ;
    }

    private:
    bool matched(string A, string B, int st)
    {
    for(int j=0; j<B.size(); j++)
    {
    if(B[j] != A[ (st+j)%A.size() ]) return false ;
    }
    return true ;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.