Guys, really need your help here.
The question was :
There are three robots named Ray, Ben and Kevin. Initially Ray has a string S of length N. while the other two robots have empty strings. We can make either of the following moves:
Move 1: Remove the first character from Ray's string and append it
to Ben's string.
Move 2: Remove the last character from Ben's string and append it
to Kevin's string.
You must perform either of the two moves mentioned above in such a way that the strings left with Ray and Ben are empty and the string left with Kevin is lexicographically the smallest. Your task is to return this lexicographically smallest string that Kevin has after completing this activity.
Note: For any two given strings, a string is said to be lexicographically smaller than the other if it comes before the other string in the dictionary
This was my code in C++.
It ran succesfully for 4 test cases out of 10.
Please help!!!
#include<bits/stdc++.h>
using namespace std;
unordered_map<string, string> mp;
string calc(string r, string b, string k) {
if(r=="" && b=="") return k;
string v1="--", v2="--";
if(mp.count(r+" "+b+" "+k)) return mp[r+" "+b+" "+k];
if(r!="") v1 = calc(r.substr(1), b+r[0], k);
if(b!="") v2 = calc(r, b.substr(0,b.length()-1), k+b.back());
if(v1=="--") return mp[r+" "+b+" "+k] = v2;
if(v2=="--") return mp[r+" "+b+" "+k] = v1;
return mp[r+" "+b+" "+k] = min(v1, v2);
}
string thegivenfunction(int n, string s) {
return calc(s,"","");
}