Split Concatenated Strings in C++


Suppose we have a list of strings, we can concatenate these strings together into a loop, where for each string we can choose to reverse it or not. Among all of the possible loops, we need to find the lexicographically largest string after cutting the loop, which will make the looped string into a regular one. Specifically, to find the lexicographically largest string, we need to experience two phases −

Concatenate all the strings into one loop, where we can reverse some strings or not and connect them in the same order as given.

Cut and make one cutting point in any place of the loop, which will make the looped string into a regular one starting from the character at the cutting point. And the job is to find the lexicographically largest one among all the possible regular strings.

So, if the input is like "abc", "xyz", then the output will be "zyxcba" as we can get the looped string like "-abcxyz-", "-abczyx-", "-cbaxyz-", "-cbazyx-”, where '-' is used to represent the looped status. The answer string came from the fourth looped one, where we can cut from the middle character 'a' and get "zyxcba".

To solve this, we will follow these steps −

  • Define a function solve(), this will take idx, array strs, rev,

  • temp := strs[idx]

  • if rev is non-zero, then −

    • reverse the array temp

  • str1 := empty string

  • str2 := empty string

  • for initialize i := 0, when i < idx, update (increase i by 1), do −

    • str1 := str1 + strs[i]

  • for initialize i := idx + 1, when i < size of strs, update (increase i by 1), do −

    • str2 := str2 + strs[i]

  • for initialize k := 0, when k < size of temp, update (increase k by 1), do −

    • newOne := substring of temp from index k to end concatenate str2 concatenate str1 concatenate substring of temp from index (0 to k-1)

    • if ret is empty or ret < newOne, then −

      • ret := newOne

  • Define a function findMax(), this will take an array strs,

  • for initialize i := 0, when i < size of strs, update (increase i by 1), do −

    • temp := strs[i]

    • reverse the array temp

    • strs[i] := (if strs[i] > temp, then strs[i], otherwise temp)

  • From the main method do the following −

  • ret := empty string

  • findMax(strs)

  • for initialize i := 0, when i < size of strs, update (increase i by 1), do −

    • solve(i, strs, false)

    • solve(i, strs, true)

  • return ret

Example

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   string ret;
   void solve(int idx, vector <string > strs, bool rev){
      string temp = strs[idx];
      if (rev)
         reverse(temp.begin(), temp.end());
      string str1 = "";
      string str2 = "";
      for (int i = 0; i < idx; i++)
         str1 += strs[i];
      for (int i = idx + 1; i < strs.size(); i++)
         str2 += strs[i];
      for (int k = 0; k < temp.size(); k++) {
         string newOne = temp.substr(k) + str2 + str1 + temp.substr(0, k);
         if (ret == "" || ret < newOne) {
            ret = newOne;
         }
      }
   }
   void findMax(vector<string>& strs){
      for (int i = 0; i < strs.size(); i++) {
         string temp = strs[i];
         reverse(temp.begin(), temp.end());
         strs[i] = strs[i] > temp ? strs[i] : temp;
      }
   }
   string splitLoopedString(vector& strs) {
      ret = "";
      findMax(strs);
      for (int i = 0; i < strs.size(); i++) {
         solve(i, strs, false);
         solve(i, strs, true);
      }
      return ret;
   }
};
main(){
   Solution ob;
   vector<string> v = {"abc", "xyz"};
   cout << (ob.splitLoopedString(v));
}

Input

{"abc", "xyz"}

Output

zyxcba

Updated on: 16-Nov-2020

384 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements