Check if alphabetical order sum of given strings are equal or not - GeeksforGeeks

06 Jan, 2022
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 notExamples:
Input: s1= “geek”, s2=”abcdefg”Output: TrueExplanation:  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++
Java
Python3
C#
Javascript
// 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 charactersbool 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 Codeint main(){    string s1 = "geek";    string s2 = "abcdefg";    cout << (findTheSum(s1, s2) ? "True" : "False");     return 0;}
// Java code to implement above approachimport 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.
# Python code for the above approach # Function to check if both strings have the# same alphabetical order sum of charactersdef 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 Codes1 = "geek"s2 = "abcdefg"if findTheSum(s1, s2) == 1:    print("True")else:    print("False") # This code is contributed by Potta Lokesh
// C# code to implement above approachusing 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
<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)Auxiliary Space: O(1) 
rakeshsahni
samim2000
shikhasingrajput
lokeshpotta20
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Data Types
Inline Functions in C++
Pure Virtual Functions and Abstract Classes in C++
Write a program to reverse an array or string
Write a program to print all permutations of a given string
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack
KMP Algorithm for Pattern Searching