Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 25 Aug, 2021

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.


Input: {“2a”, “grea3t”, “l3earning”, “geeksfor0geeks”, “p10latform”, “is1”}
Output: geeksfor0geeks is1 2a grea3t l3earning p10latform
Explanation: Since order of integer parts are: 0, 1, 2, 3, 3, 10
therefore the order of string must be:
geeksfor0geeks, is1, 2a, grea3t, l3earning, p10latform.

Input: {“love9”, “i8s”, “In5dia”}
Output: In5dia i8s love9


Approach: 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.


// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
void sortJumbledList(string jumbled[], int size)
    // Initializing a list to store pairs
    multimap<int, string> ans;
    // Iterating over JumbledList
    for (int i = 0; i < size; i++) {
        string temp = jumbled[i];
       // Finding integer part
        int number = 0;
        for (int j = 0; j < temp.size(); j++) {
            if (temp[j] >= '0' && temp[j] <= '9') {
                number *= 10;
                number += (temp[j] - '0');
        // Appending pairs
        ans.insert(pair<int, string>(number, jumbled[i]));
   // Printing sorted word of the string.
    for (auto i : ans) {
        cout << i.second << " ";
// Driver code
int main()
    string JumbledList[] = { "2a",        "grea3t",
                             "l3earning", "geeksfor0geeks",
                             "p5latform", "is1" };
    sortJumbledList(JumbledList, 6);
    return 0;
// This code is contributed by maddler.


# Python program for above approach
def SortJumbledList(JumbledList):
    # Initializing a list to store pairs
    A = []
    # Iterating over JumbledList
    for string in JumbledList:
        # Finding integer part
        integer = []
        for j in string:
            if j in {'0', '1', '2', '3', \
                         '4', '5', '6', \
                         '7', '8', '9'}:
        integer = ''.join(integer)
        # Appending pairs
        A.append((integer, string))
    # Sorting the list of pairs   
    # Printing sorted word of the string.
    for integer, string in A:
        print(string, end =' ')
# Driver Code
JumbledList = [ "2a", "grea3t", \
                "l3earning", \
                "geeksfor0geeks", \
                "p5latform", "is1" ]
# Function Call
geeksfor0geeks 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)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!