solution without KMP


  • 0
    G
    class Solution {
    public:
        string shortestPalindrome(string s) {
            string t=s,adder="";
            reverse(s.begin(),s.end());
            int i;
            for(i=0;i<s.size();i++){
                string tmp=t.substr(0,s.size()-i);
                adder=t.substr(s.size()-i);
                if(s.find(tmp,i)!=string::npos)break;
            }
            s+=adder;
            reverse(s.begin(),s.end());
            return s;
        }
    };
    

Log in to reply
 

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