concatenate string to itself to imitate loop


  • 0
    I
    class Solution {
    public:
        string best(string &s)
        {
            string s1(s);
            reverse(s1.begin(),s1.end());
            string res = max(s,s1);
            return res;
        }
        string build(vector<string>& strs, int index, char mxbeg, int len)
        {
            string b1, b2;
            b1 = strs[index];
            b2 = b1;
            reverse(b2.begin(),b2.end());
            string rest;
            for(int i = index + 1; i < index + strs.size()/2; ++i)
               rest += best(strs[i]);
            string now = "a",res;
            for(int i = 0; i < b1.size(); ++i)
            {
               if(b1[i] == mxbeg)
                  now = b1.substr(i) + rest + b1.substr(0,i);
               res = max(now,res);
            }
            for(int i = 0; i < b2.size(); ++i)
            {
               if(b2[i] == mxbeg)
                  now = b2.substr(i) + rest + b2.substr(0,i);
               res = max(now,res);
            }
            return res;
        }
        string splitLoopedString(vector<string>& strs) {
            string ans;
            if(!strs.size()) return ans;
            char mxbeg = 'a';
            int len = 0;
            for(int i = 0; i < strs.size(); ++i)
            {
               len += strs[i].size();
               for(int j = 0; j < strs[i].size(); ++j)
               {
                   mxbeg = max(mxbeg,strs[i][j]);
                   ans += strs[i];
               }
            }   
            
            strs.insert(strs.begin(),strs.begin(),strs.end());
    
            for(int i = 0; i < strs.size()/2; ++i)
            {
                  int found=strs[i].find(mxbeg);
                  if (found!=string::npos)
                  {
                     string now = build(strs,i,mxbeg,len); 
                     ans = max(now,ans);
                  }
            }
            ans.resize(len);
            return ans;
    
        }
    };
    

Log in to reply
 

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