Click here to Skip to main content
15,885,868 members
Please Sign up or sign in to vote.
1.16/5 (4 votes)
See more:
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


What I have tried:

I have tried with recursion but didn't worked
Posted
Updated 23-Jun-22 4:44am
v2
Comments
Patrice T 12-Jun-22 13:34pm    
Show your code and break a sentence to explain its problem.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
ray = input("Enter the string : ")

ben = ''
kevin = ''

ben = ray[0]                     # Do move 1
ray = ray[1:]

while ben or ray:
                         # First two if conditions are for empty strings
    if ben == '':
        ben = ray[0]             # If ben string is empty do move 1
        ray = ray[1:]
    if ray == '':                # If ray string is empty then
        kevin = kevin+ben[::-1]  # reverse ben string and add it to kevin
        break                    # break here to prevent string indexing
                                 # error in next condition

    # Below if and else conditions works only 
    # when both ray and ben are not empty
    
    if min(ray) < ben[-1] :
        ben = ben+ray[0]         # Do move 1
        ray = ray[1:]            # Remove first character from ray string
    else:
        kevin = kevin+ben[-1]    # Do move 2
        ben = ben[:-1]           # Remove last character from ben string
print(kevin)
 
Share this answer
 
v2
Comments
Ketha Venkata Sai Aravind 23-Jun-22 10:49am    
If any test cases fail comment below
C++
/* Take string length and string as input 

   Example: 6 master
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    string ray, ben = "", kevin = "";
    cin >> n >> ray; 
    
    int count = 0, k = 0, m;
    stack<char> next;
    while(count < n)
    {
        char ch = 'z';
        for(int i = k; i < n; i++)
        {
            if(ray[i] < ch)
            {
                ch = ray[i];
                m = i;
            }
        }
        
        for(int i = k; i < m; i++)
        {
            next.push(ray[i]);
        }
        
        if(next.top() < ch)
        {
            kevin = kevin + next.top();
            next.pop();
        }
        else
            kevin = kevin + ray[m];
        k = m+1;
        count++;
    }
    
    cout << kevin << endl;
    
    return 0;
}
 
Share this answer
 
Comments
Anish Gupta Jun2022 18-Jun-22 16:17pm    
write like this
bool add=false;
if(!next.empty()){
if(next.top() <= ch )
{
kevin = kevin + next.top();
next.pop();
add=true;
}
}

if(!add) kevin = kevin + ray[m];

otherwise it will cause segmentation fault
Anish Gupta Jun2022 18-Jun-22 16:41pm    
#include <bits stdc++.h="">
using namespace std;

int main()
{
int n;
string ray, ben = "", kevin = "";
cin >> n >> ray;

int count = 0, k = 0, m;
stack<char> next;
while(count < n)
{
char ch = 'z'+1;
for(int i = k; i < n; i++)
{
if(ray[i] < ch)
{
ch = ray[i];
m = i;
}
}

for(int i = k; i < m; i++)
{
next.push(ray[i]);
}
bool add=false;
if(!next.empty()){
if(next.top() <= ch )
{
kevin = kevin + next.top();
next.pop();
add=true;
}
}

if(!add){ kevin = kevin + ray[m];
k = m+1;
}
count++;
}

cout << kevin << endl;

return 0;
}

Made some changes in your code which cover edge cases
CHill60 20-Jun-22 5:06am    
A code dump without commentary is not a good answer, and doing someone's homework for them helps no-one
def move1(ray,ben):
    ben=    ben+ray[0]
    ray = ray[1:]
    return(ray,ben)
def move2(ben, kevin):
    kevin = kevin+ben[-1]
    ben = ben[:-1]
    return(ben,kevin)
ray = "vedant"
ben = ""
kevin =""

# find smallest letter in a string
def smallest(string):
    smallest = string[0]
    for letter in string:
        if letter < smallest:
            smallest = letter
    return smallest

ray,ben= move1(ray,ben)
while(ray or ben):
    if(ray and ben):
        if(smallest(ray) < smallest(ben)):
            ray,ben = move1(ray,ben)
        else:
            ben,kevin = move2(ben,kevin)
    elif(ray):
        ray,ben = move1(ray,ben)
    else:
        ben,kevin = move2(ben,kevin)

print( kevin)
 
Share this answer
 
Comments
Anish Gupta Jun2022 18-Jun-22 17:11pm    
it will give aodntv for vdoant but lexicographically smallest should be anodtv
CHill60 20-Jun-22 5:06am    
A code dump without commentary is not a good answer, and doing someone's homework for them helps no-one
#include<bits/stdc++.h>
using namespace std;

//TC:O(n^2), SC:O(n)
int main() {
	string s = "xyzbazybc"; //abbcyzzyx
	// cin >> s;
	string ans, mid;
	auto st = s.begin();
	while (st != s.end()) {
		auto itr = min_element(st, s.end());

		if (mid.length() and mid.back() <= *itr) {
			ans += mid.back();
			mid.pop_back();
		} else {
			mid += s.substr(st - s.begin(), (itr - st ) + 1);
			st = ++itr;
		}
	}
	if(mid.length()){
		reverse(mid.begin(),mid.end());
		ans+=mid;
	}
	cout<<ans;
}
 
Share this answer
 
Comments
CHill60 20-Jun-22 5:05am    
A code dump without commentary is not a good answer, and doing someone's homework for them helps no-one.
input="cba"
n=[]
o=[]
im=[]
n=[ord(i)-96 for i in input]
while len(n)>0:
  min_val=min(n)
  min_index=n.index(min_val)
  o+=[min_val]
  im+=n[:min_index]
  n=n[min_index+1:]
  if len(n)>0:
    min_val=min(n)
    min_index=n.index(min_val)
    while len(im)>0 and min_val>=im[-1]:
      o+=[im[-1]]
      im=im[:-1]
if len(im)>0:
  o+=im[::-1]  
  
op="".join(chr(i+96) for i in o)
print(op)
 
Share this answer
 
Comments
CHill60 20-Jun-22 5:05am    
A code dump without commentary is not a good answer, and doing someone's homework for them helps no-one
import string
n=int(input("Enter the Number of Elements : "))
ray=input("Enter a String : ")
ben=""
kevin=""
ray1=list(ray)
n=1
ben1=list(ray)
ben1.sort()
ray=''
ben=''
for ele in ben1:
	kevin+= ele
print(kevin)
 
Share this answer
 
Comments
Richard Deeming 23-Jun-22 6:05am    
An unexplained code-dump is not a good solution.

And you don't help anyone by doing their homework for them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900