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.
@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
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
ABA
I brought A from last before B to make it AAB. Anything wrong you can think of with this?
o(nlogn).
we will sort the list in ascending order.
then we will check for the first character which does nt match with the sorted list from the beginning of the given list.we will search this character from the end of the given list and bring it to correct position as in the sorted list
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 CodeChef: Practical coding for everyone but giving wrong answer
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).
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