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
alright now lets calculate frequency of all letters.(do it in ha table or something)
a-6,b-1,z-2
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
i think my approach is o(n)
link:CodeChef: Practical coding for everyone
with two steps :
first insert at first mismatch
second: remove at first mismatch
and than compare both solution and print that which is lesser