PROBLEM LINK:Author: Vitaliy Herasymiv DIFFICULTY:Cakewalk. PREREQUISITES:Simulation of brute force algorithm. PROBLEM:You have a string $S$ which consists of $N$ uppercase English letters. You are allowed to at most once do the following process: Choose any position in the string, remove the character at this position and insert the same character back in any other place. Find the lexicographically smallest string you can achive. QUICK EXPLANATION:Generate all possible strings, and keep track of the lexicographically smallest string. EXPLANATION:We can create all possible strings by removing a character at some position and adding it to other position. We will have one string for each removed and inserted positions. So overall there will be total $O(n^2)$ such possible strings. We can implement the process of removal and additions of character at some position in $O(n)$ time. Overall it will take $O(n^2 * n) = O(n^3)$ time. We just need to write a function Modify(), which will take input two integers $i$ and $j$, and delete the $j^{th}$ character of the given string and insert it between $i^{th}$ and $(i+1)^{th}$ character. Modify() function will generate a new string which we can get by performing the given process at most once. We can run Modify() function on all possible values of $i$ and $j$, and report the minimum one. Peseudo code can be given as below:
Solving the given problem with better complexity is an interesting task, please comment if you find such solution. Solution:Setter's solution can be found here
This question is marked "community wiki".
asked 20 Mar '15, 18:16

@amitpandeykgp , I tried solving using the following approach: Take the smallest character in the string and take it to the letfmost possible position, call this string s1 Take the greatest character in the string and take it to the rightmost possible position, call this string s2 Take the lexicographically smallest between s1 and s2. Can you point out the fault in this logic? I got WA Solution link : http://www.codechef.com/viewsolution/6566723 answered 23 Mar '15, 00:13
3
It is wrong. Counter example: ABAA: In this case, you will print AABA, but you can make it AAAB too :)
(23 Mar '15, 00:17)
@dpraveen , No, my code prints AAAB only. Link : http://ideone.com/rU6isc
(23 Mar '15, 00:23)
From my explanation , s1 will be "ABAA" and s2 will be "AAAB". And I'm taking the lexicaographically smaller of the two
(23 Mar '15, 00:30)
What are you doing if there many characters which are equal to the smallest one?
(23 Mar '15, 00:33)
What will be your output for "ZAZAZA"?
(23 Mar '15, 00:34)
In s1, Take the rightmost smallest character In s2, take the rightmost greatest character
(23 Mar '15, 00:35)
AZAZAZ
(23 Mar '15, 00:37)
3
abaaza s1 = aabaza s2 = abaaaz ans is aaabza :D
(23 Mar '15, 00:47)
Thanks :) Should've given priority to the position for s2 :(
(23 Mar '15, 01:03)
showing 5 of 9
show all

Please tell me where i am wrong. answered 23 Mar '15, 00:18

I got, i too am getting wa in above test case. answered 23 Mar '15, 00:22

include<iostream>include<cstdio>include<cstring>using namespace std; int main() { int max, n,m,i,brr[51],j,p,a,b; char arr[51],temp; scanf("%d",&n); while(n) { max=0; scanf("%d",&m); for(i=0;i<=m;i++){ scanf("%c",&arr[i]); brr[i]=(int)arr[i]; } for(i=1;i<=m;i++) { for(j=i+1;j<=m;j++) if((brr[i]brr[j])>0) {p=brr[i]brr[j]; if(max<p){ max=p;a=i;b=j;} } } arr[a]=arr[b]; for(j=a+1;j<=b;j++) arr[j]=(char)brr[j1]; for(i=1;i<=m;i++) printf("%c",arr[i]); printf("\n"); } return 0; } what is wrong with my code please explain answered 23 Mar '15, 00:23

i didn't get the logic of inserting jth character between ith and (i+1)th.Please Explain. answered 23 Mar '15, 01:14
1
Every language provides some fuctions to do such things. See http://www.cplusplus.com/reference/string/string/insert/ for C++.
(23 Mar '15, 01:17)

$\mathcal{O}(n^2)$ algorithm
You can implement both the cases in $\mathcal{O}(n^2)$ easily. I have not given a proper thought, but a $\mathcal{O}(n)$ can be made along the same lines. answered 23 Mar '15, 01:29
I tried the same approach, getting WA. Here: http://www.codechef.com/viewsolution/6567816
(24 Mar '15, 00:35)

I found the minimum position where the character was different from character at the same position when string was sorted. I brought that character from the furthest position in the string. Ex: katrina I brought a from last to make it akatrin. AAB I didn't do anything :P ABA I brought A from last before B to make it AAB. Anything wrong you can think of with this? answered 23 Mar '15, 03:35

yes,,, its incorrect ,,, for example ABAA ,,,, ur answer is AABA bt answer is AAAB answered 23 Mar '15, 03:48

Your explanation doesn't consider the case of inserting jth character at the beginning of the string. answered 23 Mar '15, 14:38

I don't know what happened to me last night not to solve this. Now I see my mistake the inner for loop should be 0<= j < n answered 23 Mar '15, 16:35

hey what about this approach get the ascending order of given string compare the sorted string one by one and then the first character that differs in given string then that of sorted one is inserted in and we remove the last occurence of that character. please suggest if any correction my code is this http://www.codechef.com/viewsolution/6570974 but giving wrong answer answered 23 Mar '15, 17:09
hi @dpraveen could you please tell me where am i going wrong
(24 Mar '15, 16:04)

@dpraveen I tried exactly what u said.. Correct me if i am wrong. http://www.codechef.com/viewsolution/6570234 answered 23 Mar '15, 22:32

To insert character use str.insert() and REMEMBER to erase that character before inserting.Use str.erase().Also when using str.erase please use the position parameter as well as iterator parameter(here it is 1). answered 23 Mar '15, 23:12

O(n*n) Take every character and assume rest is sorted and insert this char in an appropriate position,lexicographically compare with the given string and then find out the minimum one Code in Python : http://www.codechef.com/viewsolution/6564893 answered 24 Mar '15, 00:54

Still not able to figure out why my answer is wrong. Please provide some test cases. `#include<iostream> include<string.h>using namespace std; int main() { //cout<<strcmp("fff","ddd"); int="" t,i,j,k,len;="" char="" str[52],temp[52],chr,min[52];="" cin="">>t; while(t) { cin>>len>>str; strcpy(min,str); for(i=0;i<len;i++) {="" chr="str[i];" for(j="0;j<len;j++)" {="" strcpy(temp,str);="" if(i="=j)" continue;="" else="" if(i<j)="" {="" for(k="i;k<j;k++)" {="" temp[k]="str[k+1];" }="" temp[k]="chr;" }="" else="" {="" for(k="i;k">j;k) { temp[k]=str[k1]; } temp[k]=chr; } //cout<<temp<<endl; if(strcmp(temp,min)==1) strcpy(min,temp);1 } } cout<<min<<endl; } return 0; } ` answered 24 Mar '15, 18:39

Someone please help. Please someone give me a test case on which my solution fails. Here is my code. http://www.codechef.com/viewsolution/6574661 answered 24 Mar '15, 20:14

Anyone wanna help me find out counter example for my code to the problem? http://www.codechef.com/viewsolution/6574368 answered 24 Mar '15, 20:55

i can be done in o(n). lets take an example abaaazzaa alright now lets calculate frequency of all letters.(do it in ha table or something) a6,b1,z2 alright now , we store the leftmost a's position that is 8(i=0 based index). so its obvious that at index 1 , we need to replace b with a, which can be done in two ways. 1st way we take the last a and inject it before b. 2nd way we keep shifting b towards the end until there is a letter which is bigger than b. the key obs is that , the answer has to be one of it , there is no other option, so what we do is buid both the string seperatly (both are o(n)). now s1=aabaaazza and s2=aaaabzza so the answer is s2. if there is any cornor case im missing , pls lemme know :) answered 26 Jun '16, 18:10

@amitpandeykgp i solved this question using C++ stl ( insert and erase function).I just want to know whether complexity of insert and erase is O(1) or O(n)?? Here is my solution : https://www.codechef.com/viewsolution/10649315 So, overall Complexity is O(n^3) or O(n^2) ?? answered 01 Jul '16, 14:10

all test cases given above are working then also i am getting a wrong ans
https://www.codechef.com/viewsolution/10858733>https://www.codechef.com/viewsolution/10858733
answered 20 Jul '16, 06:27

i think my approach is o(n) link:https://www.codechef.com/viewsolution/14979205 with two steps : first insert at first mismatch second: remove at first mismatch and than compare both solution and print that which is lesser answered 15 Aug '17, 16:58

I solved it with O(N^2) per test case during the contest itself.
Here is my solution link :) http://www.codechef.com/viewsolution/6562897