- Sorting, Strings

Sort given sentence on the basis of integer present in every string

Given a jumbled sentence as a list of strings, the task is to print the sorted sentence of strings on basis of presence of single integer in every string. If two string has same integer sort them lexicographically.Examples:Input: {“2a”, “grea3t”, “l3earning”, “geeksfor0geeks”, “p10latform”, “is1”}Output: geeksfor0geeks is1 2a grea3t l3earning p10latformExplanation: Since order of integer parts are: 0, 1, 2, 3, 3, 10therefore the order of string must be:geeksfor0geeks, is1, 2a, grea3t, l3earning, p10latform.Input: {“love9”, “i8s”, “In5dia”}Output: In5dia i8s love9Approach: The problem can be solved using greedy algorithm. we will create a list of pairs, the first value of pairs will hold integer part of string and second value of pairs will hold string as it is and then we will sort this list of pairs in ascending order, so that the string having lower valued integer will earlier in list. follow the steps below to solve the problem:Create a list of pairs say A, in pair 1st values will be integer in string and 2nd value will be string as it is.Sort A in increasing order.Iterate over every pair of A and print 2nd value of pair.Python3def SortJumbledList(JumbledList):              A = []              for string in JumbledList:                        integer = []        for j in string:            if j in {‘0’, ‘1’, ‘2’, ‘3’,                          ‘4’, ‘5’, ‘6’,                          ‘7’, ‘8’, ‘9’}:                integer.append(j)        integer = ”.join(integer)                          A.append((integer, string))                  A.sort()              for integer, string in A:        print(string, end =’ ‘)          JumbledList = [ “2a”, “grea3t”,                 “l3earning”,                 “geeksfor0geeks”,                 “p5latform”, “is1” ]  SortJumbledList(JumbledList)Outputgeeksfor0geeks is1 2a grea3t l3earning p5latform Time Complexity: O(N*M), where N is the length of JumbledList and M is the length of string.Auxiliary Space: O(N*M)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.