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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if both strings have the // same alphabetical order sum of characters bool findTheSum(string s1, string s2) { int sum1 = 0; int sum2 = 0; int n = s1.length(); int m = s2.length(); // Add value of char in sum1 for ( int i = 0; i < n; i++) { sum1 += s1[i] - 'a' + 1; } // Add value of char in sum2 for ( int i = 0; i < m; i++) { sum2 += s2[i] - 'a' + 1; } // Check sum1 are equal to sum2 or not if (sum1 == sum2) { return true ; } else { return false ; } } // Driver Code int main() { string s1 = "geek" ; string s2 = "abcdefg" ; cout << (findTheSum(s1, s2) ? "True" : "False" ); return 0; } |
Java
// Java code to implement above approach import java.util.*; public class GFG { // Function to check if both strings have the // same alphabetical order sum of characters static boolean findTheSum(String s1, String s2) { int sum1 = 0 ; int sum2 = 0 ; int n = s1.length(); int m = s2.length(); // Add value of char in sum1 for ( int i = 0 ; i < n; i++) { sum1 += s1.charAt(i) - 'a' + 1 ; } // Add value of char in sum2 for ( int i = 0 ; i < m; i++) { sum2 += s2.charAt(i) - 'a' + 1 ; } // Check sum1 are equal to sum2 or not if (sum1 == sum2) { return true ; } else { return false ; } } // Driver code public static void main(String args[]) { String s1 = "geek" ; String s2 = "abcdefg" ; System.out.println((findTheSum(s1, s2) ? "True" : "False" )); } } // This code is contributed by Samim Hossain Mondal. |
Python3
# Python code for the above approach # Function to check if both strings have the # same alphabetical order sum of characters def findTheSum(s1, s2): sum1 = 0 sum2 = 0 n = len (s1) m = len (s2) # Add value of char in sum1 for i in range (n): sum1 + = ord (s1[i]) - ord ( 'a' ) + 1 # Add value of char in sum2 for i in range (m): sum2 + = ord (s2[i]) - ord ( 'a' ) + 1 # Check sum1 are equal to sum2 or not if (sum1 = = sum2): return 1 else : return 0 # Driver Code s1 = "geek" s2 = "abcdefg" if findTheSum(s1, s2) = = 1 : print ( "True" ) else : print ( "False" ) # This code is contributed by Potta Lokesh |
C#
// C# code to implement above approach using System; public class GFG { // Function to check if both strings have the // same alphabetical order sum of characters static bool findTheSum(String s1, String s2) { int sum1 = 0; int sum2 = 0; int n = s1.Length; int m = s2.Length; // Add value of char in sum1 for ( int i = 0; i < n; i++) { sum1 += s1[i] - 'a' + 1; } // Add value of char in sum2 for ( int i = 0; i < m; i++) { sum2 += s2[i] - 'a' + 1; } // Check sum1 are equal to sum2 or not if (sum1 == sum2) { return true ; } else { return false ; } } // Driver code public static void Main(String []args) { String s1 = "geek" ; String s2 = "abcdefg" ; Console.WriteLine((findTheSum(s1, s2) ? "True" : "False" )); } } // This code is contributed by shikhasingrajput |
Javascript
<script> // JavaScript program for the above approach // Function to check if both strings have the // same alphabetical order sum of characters const findTheSum = (s1, s2) => { let sum1 = 0; let sum2 = 0; let n = s1.length; let m = s2.length; // Add value of char in sum1 for (let i = 0; i < n; i++) { sum1 += s1.charCodeAt(i) - 'a' .charCodeAt(0) + 1; } // Add value of char in sum2 for (let i = 0; i < m; i++) { sum2 += s2.charCodeAt(i) - 'a' .charCodeAt(0) + 1; } // Check sum1 are equal to sum2 or not if (sum1 == sum2) { return true ; } else { return false ; } } // Driver Code let s1 = "geek" ; let s2 = "abcdefg" ; if (findTheSum(s1, s2)) document.write( "True" ); else document.write( "False" ); // This code is contributed by rakeshsahni </script> |
True
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" ); } } // This code is contributed by prasad264 |
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" ); |
True
Time Complexity: O(n)
Auxiliary Space: O(1)