Given an array of strings, we need to sort the array in increasing order of string lengths, using Map Data Structure. Examples:
Input: str[] = {“GeeksforGeeeks”, “I”, “from”, “am”} Output: I am from neveropen Input: str[] = {“You”, “are”, “beautiful”, “looking”} Output: You are looking beautiful
Approach: The given approach is for Java using TreeMap
- Take a TreeMap which will contain the length of the string as the key and an ArrayList of Strings that has the same length that of the key.
- Since we know that a TreeMap is already sorted so after inserting into the TreeMap we again fetch the values(the Strings from the ArrayList) one by one into the Strings Array.
Below is the implementation of the above approach:
C++
// C++ program to sort an Array of // Strings according to their lengths // using Map #include <bits/stdc++.h> using namespace std; // Function to Sort the array of string // according to lengths using Map vector<string> sortS(vector<string>& str, int n) { map< int , vector<string> > m; for ( int i = 0; i < n; i++) { m[str[i].length()].push_back(str[i]); } int temp = 0; for ( auto i : m) { for ( int j = 0; j < i.second.size(); j++) { str[temp] = i.second[j]; temp++; } } return str; } // Function to print the sorted array of string void printArraystring(vector<string> str, int n) { for ( int i = 0; i < n; i++) cout << str[i] << " " ; } // Driver function int main() { vector<string> arr; arr = { "neveropen" , "I" , "from" , "am" }; int n = arr.size(); // Function to perform sorting arr = sortS(arr, n); // Calling the function to print result printArraystring(arr, n); } // This code is contributed by akashish__ |
Java
// Java program to sort an Array of // Strings according to their lengths // using Map import java.util.*; import java.util.Map.Entry; import java.io.*; public class GFG { // Function to Sort the array of string // according to lengths using Map static String[] sort(String[] str, int n) { TreeMap<Integer, ArrayList<String> > map = new TreeMap<Integer, ArrayList<String> >(); for ( int i = 0 ; i < n; i++) { map.putIfAbsent(str[i].length(), new ArrayList<String>()); map.get(str[i].length()).add(str[i]); } int temp = 0 ; for (Entry<Integer, ArrayList<String> > e : map.entrySet()) { for ( int i = 0 ; i < e.getValue().size(); i++) { str[temp] = e.getValue().get(i); temp++; } } return str; } // Function to print the sorted array of string static void printArraystring(String str[], int n) { for ( int i = 0 ; i < n; i++) System.out.print(str[i] + " "); } // Driver function public static void main(String args[]) { String[] arr = { "neveropen", "I", "from", "am" }; int n = arr.length; // Function to perform sorting arr = sort(arr, n); // Calling the function to print result printArraystring(arr, n); } } |
Python3
from collections import defaultdict # Function to Sort the array of string # according to lengths using Map def sort( str , n): map = defaultdict( list ) for i in range (n): map [ len ( str [i])].append( str [i]) return [val for key in sorted ( map ) for val in map [key]] # Driver function arr = [ "neveropen" , "I" , "from" , "am" ] n = len (arr) # Function to perform sorting arr = sort(arr, n) # Calling the function to print result print (arr) # This code is contributed by akashish__ |
C#
using System; using System.Collections.Generic; using System.Linq; public class GFG { // Function to Sort the array of string // according to lengths using Map public static string [] sort( string [] str, int n) { var map = new SortedDictionary< int , List< string >>(); for ( int i = 0; i < n; i++) { map[str[i].Length] = new List< string >(); } for ( int i = 0; i < n; i++) { map[str[i].Length].Add(str[i]); } var temp = 0; foreach (KeyValuePair< int , List< string >> e in map) { for ( int i = 0; i < e.Value.Count; i++) { str[temp] = e.Value[i]; temp++; } } return str; } // Function to print the sorted array of string public static void printArraystring( string [] str, int n) { for ( int i = 0; i < n; i++) { Console.Write(str[i] + " " ); } } // Driver function public static void Main( string [] args) { string [] arr = { "neveropen" , "I" , "from" , "am" }; var n = arr.Length; // Function to perform sorting arr = GFG.sort(arr, n); // Calling the function to print result GFG.printArraystring(arr, n); } } // This code is contributed by aadityaburujwale. |
Javascript
// Function to Sort the array of string // according to lengths using Map function sort(str, n) { let map = new Map(); for (let i = 0; i < n; i++) { let list = map.get(str[i].length); if (list === undefined) { list = []; list.push(str[i]); map.set(str[i].length, list); } else { list.push(str[i]); map.set(str[i].length, list); } } let sortedKeys = Array.from(map.keys()).sort((a, b) => a - b); let result = []; for (let key of sortedKeys) { result = result.concat(map.get(key)); } return result; } // Driver function let arr = [ "neveropen" , "I" , "from" , "am" ]; let n = arr.length; // Function to perform sorting arr = sort(arr, n); // Calling the function to print result console.log(arr); // This code is contributed by akashish. |
I am from neveropen
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!