Sort an array of strings in increasing order of sum of ASCII values of characters
Last Updated :
08 Feb, 2024
Given an array arr[] consisting of N strings, the task is to sort the strings in increasing order of the sum of the ASCII (American Standard Code for Information Interchange) value of their characters.
Examples:
Input: arr[] = {“for”, “geeks”, “app”, “best”}
Output: app for best geeks
Explanation:
Sum of ASCII values of characters of each string is: {327, 527, 321, 430}.
Hence, the sorted order of strings is {“app”, “for”, “best”, “geeks”}.
Input: arr[] = {“geeksforgeeks”, “a”, “computer”, “science”, “portal”, “for”, “geeks”}
Output: a for geeks portal science computer geeksforgeeks
Explanation:
Sum of ASCII values of characters of each string is: {1381, 97, 879, 730, 658, 327, 527}.
Hence, the sorted order is {“a”, “for”, “geeks”, “portal”, “science”, “computer”, “geeksforgeeks”}.
Approach: The idea is to use an auxiliary array to store pairs of strings and their respective sum of ASCII values of characters. Then, sort the array on the basis of the first value in the pair, and then print the sorted strings. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getValue(string s)
{
int sum = 0;
for ( int i = 0; i < s.size(); i++) {
sum += s[i];
}
return sum;
}
void sortStrings(string arr[], int n)
{
vector<pair< int , string> > v;
for ( int i = 0; i < n; i++) {
int val = getValue(arr[i]);
v.push_back({ val, arr[i] });
}
sort(v.begin(), v.end());
for ( int i = 0; i < n; i++) {
cout << v[i].second << " " ;
}
}
int main()
{
string arr[] = { "geeks" , "for" , "app" , "best" };
int n = 4;
sortStrings(arr, n);
return 0;
}
|
Java
import java.util.*;
class pair
{
int first;
String second;
pair( int first,String second)
{
this .first = first;
this .second = second;
}
}
class Gfg
{
public static int getValue(String s)
{
int sum = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
sum += s.charAt(i);
}
return sum;
}
public static void sortStrings(String arr[], int n)
{
ArrayList<pair> v = new ArrayList<pair>();
for ( int i = 0 ; i < n; i++) {
int val = getValue(arr[i]);
v.add( new pair(val,arr[i]));
}
Collections.sort(v,(a,b)->a.first-b.first);
for ( int i = 0 ; i < n; i++) {
System.out.print(v.get(i).second+ " " );
}
}
public static void main(String[] args) {
String arr[] = { "geeks" , "for" , "app" , "best" };
int n = 4 ;
sortStrings(arr,n);
}
}
|
Python3
def getValue(s):
sum = 0
for i in range ( len (s)):
sum + = ord (s[i])
return sum
def sortStrings(arr, n):
v = []
for i in range (n):
val = getValue(arr[i])
v.append([val, arr[i]])
v = sorted (v)
for i in range (n):
print (v[i][ 1 ], end = " " )
if __name__ = = '__main__' :
arr = [ "geeks" , "for" , "app" , "best" ]
n = 4
sortStrings(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int getValue(String s)
{
int sum = 0;
for ( int i = 0; i < s.Length; i++)
{
sum += s[i];
}
return sum;
}
static void sortStrings(String[] arr, int n)
{
List<Tuple< int ,
String>> v = new List<Tuple< int ,
String>>();
for ( int i = 0; i < n; i++)
{
int val = getValue(arr[i]);
v.Add( new Tuple< int , String>(val, arr[i]));
}
v.Sort();
for ( int i = 0; i < n; i++)
{
Console.Write(v[i].Item2 + " " );
}
}
static public void Main()
{
String[] arr = { "geeks" , "for" , "app" , "best" };
int n = 4;
sortStrings(arr, n);
}
}
|
Javascript
<script>
function getValue(s)
{
let sum = 0;
for (let i = 0; i < s.length; i++) {
sum += s[i].charCodeAt(0);
}
return sum;
}
function sortStrings(arr, n)
{
let v = [];
for (let i = 0; i < n; i++) {
let val = getValue(arr[i]);
v.push([ val, arr[i] ]);
}
v.sort();
for (let i = 0; i < n; i++) {
document.write(v[i][1] + " " );
}
}
let arr = [ "geeks" , "for" , "app" , "best" ];
let n = 4;
sortStrings(arr, n);
</script>
|
Output
app for best geeks
Time Complexity: O(N*log(N) + N*M), where M is the length of the longest string in the array arr[].
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...