Given a jumbled sentence as a list of strings, the task is to print the sorted sentence of strings on basis of the presence of a single integer in every string. If two string has the same integer sort them lexicographically.
Examples:
Input : {"2a", "grea3t", "l3earning", "neveropenfor0neveropen", "p10latform", "is1"}
Output: neveropenfor0neveropen 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:
neveropenfor0neveropen, is1, 2a, grea3t, l3earning, p10latform.
Input : {"love9", "i8s", "In5dia"}
Output: In5dia i8s love9
Approach 1: The problem can be solved using greedy algorithm. we will create a list of pairs, the first value of pairs will hold the integer part of the string and the 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 an integer in string and 2nd value will be a string as it is.
- Sort A in increasing order.
- Iterate over every pair of A and print the 2nd value of the pair.
C++
#include <bits/stdc++.h>
using namespace std;
void sortJumbledList(string jumbled[], int size)
{
multimap< int , string> ans;
for ( int i = 0; i < size; i++) {
string temp = jumbled[i];
int number = 0;
for ( int j = 0; j < temp.size(); j++) {
if (temp[j] >= '0' && temp[j] <= '9' ) {
number *= 10;
number += (temp[j] - '0' );
}
}
ans.insert(pair< int , string>(number, jumbled[i]));
}
for ( auto i : ans) {
cout << i.second << " " ;
}
}
int main()
{
string JumbledList[] = { "2a" , "grea3t" ,
"l3earning" , "neveropenfor0neveropen" ,
"p5latform" , "is1" };
sortJumbledList(JumbledList, 6);
return 0;
}
|
Java
import java.util.*;
public class JumbledListSort {
public static void sortJumbledList(String jumbled[], int size)
{
Map<Integer, String> ans = new TreeMap<>();
for ( int i = 0 ; i < size; i++) {
String temp = jumbled[i];
int number = 0 ;
for ( int j = 0 ; j < temp.length(); j++) {
if (Character.isDigit(temp.charAt(j))) {
number *= 10 ;
number += (temp.charAt(j) - '0' );
}
}
ans.put(number, jumbled[i]);
}
for (Map.Entry<Integer, String> entry : ans.entrySet()) {
System.out.print(entry.getValue() + " " );
}
}
public static void main(String[] args)
{
String JumbledList[] = { "2a" , "grea3t" ,
"l3earning" , "neveropenfor0neveropen" ,
"p5latform" , "is1" };
sortJumbledList(JumbledList, 6 );
}
}
|
Python3
def 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" ,
"neveropenfor0neveropen" ,
"p5latform" , "is1" ]
SortJumbledList(JumbledList)
|
C#
using System;
using System.Collections.Generic;
public class JumbledListSort {
public static void sortJumbledList( string [] jumbled, int size)
{
SortedDictionary< int , string > ans = new SortedDictionary< int , string >();
for ( int i = 0; i < size; i++) {
string temp = jumbled[i];
int number = 0;
for ( int j = 0; j < temp.Length; j++) {
if (Char.IsDigit(temp[j])) {
number *= 10;
number += (temp[j] - '0' );
}
}
if (ans.ContainsKey(number)) {
int index = 1;
while (ans.ContainsKey(number + index)) {
index++;
}
ans.Add(number + index, jumbled[i]);
} else {
ans.Add(number, jumbled[i]);
}
}
foreach (KeyValuePair< int , string > entry in ans) {
Console.Write(entry.Value + " " );
}
}
public static void Main()
{
string [] JumbledList = { "2a" , "grea3t" ,
"l3earning" , "neveropenfor0neveropen" ,
"p5latform" , "is1" };
sortJumbledList(JumbledList, 6);
}
}
|
Javascript
function sortJumbledList(jumbled, size) {
let ans = new Map();
for (let i = 0; i < size; i++) {
let temp = jumbled[i];
let number = 0;
for (let j = 0; j < temp.length; j++) {
if (temp[j] >= '0' && temp[j] <= '9' ) {
number *= 10;
number += (temp.charCodeAt(j) - '0' .charCodeAt(0));
}
}
ans.set(number, jumbled[i]);
}
for (let [key, value] of ans) {
console.log(value + " " );
}
}
function main() {
let JumbledList = [ "2a" , "grea3t" , "l3earning" , "neveropenfor0neveropen" , "p5latform" , "is1" ];
sortJumbledList(JumbledList, JumbledList.length);
}
main();
|
Output
neveropenfor0neveropen is1 2a grea3t l3earning p5latform
Time Complexity: O(N*M*log(N)), where N is the length of JumbledList and M is the string length. (N*log(N) for sorting/multimap.)
Auxiliary Space: O(N*M)
Approach 2: We can use a dictionary to store the index of the string while iterating through the JumbledList, where the key will be string and value will be index(one can also use string as value and index as key).
- Create a dictionary/Hashmap as mentioned above.
- Create an empty list namely of the size of the original JumbledList.
- Iterate over dictionary items and assign strings(keys) to index(values) in the above-created list.
C++
#include <bits/stdc++.h>
#include <string>
using namespace std;
void sortJumbledList(string jumbled[], int size)
{
unordered_map<string, int > mp;
for ( int i = 0; i < size; i++) {
string temp = jumbled[i];
for ( int j = 0; j < temp.size(); j++) {
if (temp[j] >= '0' && temp[j] <= '9' ) {
mp[temp] = temp[j] - '0' ;
}
}
}
string ordered[size] = {};
for ( const auto & p : mp) {
string word = p.first;
int index = p.second;
ordered[index] = word;
}
for ( const auto & word : ordered) {
cout << word << " " ;
}
}
int main()
{
string JumbledList[] = { "2a" , "grea3t" ,
"l3earning" , "neveropenfor0neveropen" ,
"p5latform" , "is1" };
sortJumbledList(JumbledList, 6);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void SortJumbledList(String[] JumbledList) {
Map<String, Integer> d = new HashMap<>();
for (String string : JumbledList) {
for ( char ch : string.toCharArray()) {
if (Character.isDigit(ch)) {
d.put(string, Character.getNumericValue(ch));
}
}
}
String[] orderedList = new String[JumbledList.length];
for (Map.Entry<String, Integer> entry : d.entrySet()) {
orderedList[entry.getValue()] = entry.getKey();
}
System.out.println(String.join( " " , orderedList));
}
public static void main(String[] args) {
String[] JumbledList = { "2a" , "grea3t" , "l3earning" , "neveropenfor0neveropen" , "p5latform" , "is1" };
SortJumbledList(JumbledList);
}
}
|
Python3
def SortJumbledList(JumbledList):
d = {}
for string in JumbledList:
for ch in string:
if ch.isdigit():
d[string] = int (ch)
orderedList = [''] * len (JumbledList)
for string,index in d.items():
orderedList[index] = string
print ( * orderedList)
JumbledList = [ "2a" , "grea3t" , \
"l3earning" , \
"neveropenfor0neveropen" , \
"p5latform" , "is1" ]
SortJumbledList(JumbledList)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void SortJumbledList( string [] jumbled)
{
Dictionary< string , int > mp = new Dictionary< string , int >();
for ( int i = 0; i < jumbled.Length; i++)
{
string temp = jumbled[i];
foreach ( char c in temp)
{
if ( char .IsDigit(c))
{
mp[temp] = int .Parse(c.ToString());
}
}
}
string [] ordered = new string [jumbled.Length];
foreach ( var kvp in mp)
{
string word = kvp.Key;
int index = kvp.Value;
ordered[index] = word;
}
foreach ( string word in ordered)
{
Console.Write(word + " " );
}
}
static void Main( string [] args)
{
string [] jumbledList = { "2a" , "grea3t" , "l3earning" , "neveropenfor0neveropen" , "p5latform" , "is1" };
SortJumbledList(jumbledList);
}
}
|
Javascript
function SortJumbledList(JumbledList) {
const d = {};
for (const string of JumbledList) {
for (const ch of string) {
if (ch >= '0' && ch <= '9' ) {
d[string] = parseInt(ch);
}
}
}
const orderedList = new Array(JumbledList.length).fill( '' );
for (const [string, index] of Object.entries(d)) {
orderedList[index] = string;
}
console.log(...orderedList);
}
const JumbledList = [ "2a" , "grea3t" ,
"l3earning" ,
"neveropenfor0neveropen" ,
"p5latform" , "is1"
];
SortJumbledList(JumbledList);
|
Output
neveropenfor0neveropen is1 2a l3earning p5latform
Time Complexity: O(N*M), where N is the length of JumbledList and M is the string length.
Auxiliary Space: O(N*M)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!