Check if alphabetical order sum of given strings are equal or not
Last Updated :
11 Apr, 2023
Given two strings s1 and s2, the task is to check if the alphabetical order sum of characters of two given strings are equal or not
Examples:
Input: s1= “geek”, s2=”abcdefg”
Output: True
Explanation: Alphabetical order sum of characters of both the strings are:
- s1= 7+5+5+11 = 28
- s2= 1+2+3+4+5+6+7 = 28
Input: s1= “bad”, s2=”good”
Output: False
Approach: The task can be solved by finding the alphabetical order sum of characters by simply iterating over the string. For each character, add the ASCII value of the current character + 1 in the resultant answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool findTheSum(string s1, string s2)
{
int sum1 = 0;
int sum2 = 0;
int n = s1.length();
int m = s2.length();
for ( int i = 0; i < n; i++) {
sum1 += s1[i] - 'a' + 1;
}
for ( int i = 0; i < m; i++) {
sum2 += s2[i] - 'a' + 1;
}
if (sum1 == sum2) {
return true ;
}
else {
return false ;
}
}
int main()
{
string s1 = "geek" ;
string s2 = "abcdefg" ;
cout << (findTheSum(s1, s2) ? "True" : "False" );
return 0;
}
|
Java
import java.util.*;
public class GFG {
static boolean findTheSum(String s1, String s2)
{
int sum1 = 0 ;
int sum2 = 0 ;
int n = s1.length();
int m = s2.length();
for ( int i = 0 ; i < n; i++) {
sum1 += s1.charAt(i) - 'a' + 1 ;
}
for ( int i = 0 ; i < m; i++) {
sum2 += s2.charAt(i) - 'a' + 1 ;
}
if (sum1 == sum2) {
return true ;
}
else {
return false ;
}
}
public static void main(String args[])
{
String s1 = "geek" ;
String s2 = "abcdefg" ;
System.out.println((findTheSum(s1, s2) ? "True" : "False" ));
}
}
|
Python3
def findTheSum(s1, s2):
sum1 = 0
sum2 = 0
n = len (s1)
m = len (s2)
for i in range (n):
sum1 + = ord (s1[i]) - ord ( 'a' ) + 1
for i in range (m):
sum2 + = ord (s2[i]) - ord ( 'a' ) + 1
if (sum1 = = sum2):
return 1
else :
return 0
s1 = "geek"
s2 = "abcdefg"
if findTheSum(s1, s2) = = 1 :
print ( "True" )
else :
print ( "False" )
|
C#
using System;
public class GFG {
static bool findTheSum(String s1, String s2)
{
int sum1 = 0;
int sum2 = 0;
int n = s1.Length;
int m = s2.Length;
for ( int i = 0; i < n; i++) {
sum1 += s1[i] - 'a' + 1;
}
for ( int i = 0; i < m; i++) {
sum2 += s2[i] - 'a' + 1;
}
if (sum1 == sum2) {
return true ;
}
else {
return false ;
}
}
public static void Main(String []args)
{
String s1 = "geek" ;
String s2 = "abcdefg" ;
Console.WriteLine((findTheSum(s1, s2) ? "True" : "False" ));
}
}
|
Javascript
<script>
const findTheSum = (s1, s2) => {
let sum1 = 0;
let sum2 = 0;
let n = s1.length;
let m = s2.length;
for (let i = 0; i < n; i++) {
sum1 += s1.charCodeAt(i) - 'a' .charCodeAt(0) + 1;
}
for (let i = 0; i < m; i++) {
sum2 += s2.charCodeAt(i) - 'a' .charCodeAt(0) + 1;
}
if (sum1 == sum2) {
return true ;
}
else {
return false ;
}
}
let s1 = "geek" ;
let s2 = "abcdefg" ;
if (findTheSum(s1, s2)) document.write( "True" );
else document.write( "False" );
</script>
|
Time Complexity: O(N)+O(M) // N is the length of the first string and M is the length of the second string
Auxiliary Space: O(1)
Approach:
This approach uses two integer arrays to count the frequency of each character in both strings. Then, it calculates the alphabetical order sum separately for both strings by multiplying the count of each character with its corresponding alphabetical order value. Finally, it compares the two sum variables to check if they are equal or not.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool findTheSum(string s1, string s2) {
int count1[26] = {0};
int count2[26] = {0};
for ( char c : s1) {
count1++;
}
for ( char c : s2) {
count2++;
}
int sum1 = 0, sum2 = 0;
for ( int i = 0; i < 26; i++) {
sum1 += (i + 1) * count1[i];
sum2 += (i + 1) * count2[i];
}
return sum1 == sum2;
}
int main() {
string s1 = "geek" ;
string s2 = "abcdefg" ;
cout << (findTheSum(s1, s2) ? "True" : "False" ) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static boolean findTheSum(String s1, String s2) {
int [] count1 = new int [ 26 ];
int [] count2 = new int [ 26 ];
for ( char c : s1.toCharArray()) {
count1++;
}
for ( char c : s2.toCharArray()) {
count2++;
}
int sum1 = 0 , sum2 = 0 ;
for ( int i = 0 ; i < 26 ; i++) {
sum1 += (i + 1 ) * count1[i];
sum2 += (i + 1 ) * count2[i];
}
return sum1 == sum2;
}
public static void main(String[] args) {
String s1 = "geek" ;
String s2 = "abcdefg" ;
System.out.println(findTheSum(s1, s2) ? "True" : "False" );
}
}
|
Python3
def find_the_sum(s1: str , s2: str ) - > bool :
count1 = [ 0 ] * 26
count2 = [ 0 ] * 26
for c in s1:
count1[ ord (c) - ord ( 'a' )] + = 1
for c in s2:
count2[ ord (c) - ord ( 'a' )] + = 1
sum1 = sum ((i + 1 ) * count1[i] for i in range ( 26 ))
sum2 = sum ((i + 1 ) * count2[i] for i in range ( 26 ))
return sum1 = = sum2
s1 = "geek"
s2 = "abcdefg"
print ( "True" if find_the_sum(s1, s2) else "False" )
|
C#
using System;
public class GFG {
static bool findTheSum( string s1, string s2) {
int [] count1 = new int [26];
int [] count2 = new int [26];
foreach ( char c in s1) {
count1++;
}
foreach ( char c in s2) {
count2++;
}
int sum1 = 0, sum2 = 0;
for ( int i = 0; i < 26; i++) {
sum1 += (i + 1) * count1[i];
sum2 += (i + 1) * count2[i];
}
return sum1 == sum2;
}
static public void Main( string [] args) {
string s1 = "geek" ;
string s2 = "abcdefg" ;
Console.WriteLine(findTheSum(s1, s2) ? "True" : "False" );
}
}
|
Javascript
function findTheSum(s1, s2) {
let count1 = new Array(26).fill(0);
let count2 = new Array(26).fill(0);
for (let c of s1) {
count1++;
}
for (let c of s2) {
count2++;
}
let sum1 = 0, sum2 = 0;
for (let i = 0; i < 26; i++) {
sum1 += (i + 1) * count1[i];
sum2 += (i + 1) * count2[i];
}
return sum1 === sum2;
}
let s1 = "geek" ;
let s2 = "abcdefg" ;
console.log(findTheSum(s1, s2) ? "True" : "False" );
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...