C++ working solution


  • 0
    M
    class Solution {
    public:
        bool repeatedSubstringPattern(string str) {
        unsigned long int str_len = str.length();
        
        string temp;
        for(int i = 0; i < str_len / 2; ++i){
            bool result = true;
            if(str[0] == str[i + 1]){
                temp = str.substr(0, i + 1);
                if(str_len % (i + 1) != 0){
                    result = false;
                }else{
                    for(int j = 0; j < str_len / (i + 1); ++j){
                        if(str.compare((i + 1) * j, i + 1, temp) != 0){
                            result = false;
                            break;
                        }
                    }
                }
            }else{
                result = false;
            }
            if(result){
                return true;
            }
        }
        return false;
        }
    };
    

Log in to reply
 

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