Weight of strings | Practice | GeeksforGeeks

You are given two strings S1 and S2. You need to find weights of both strings and compare them. The weight of a string can be obtained by adding individual weights of the characters that make the string. The weight of individual characters are the position on which they occur in the english alphabets table; for eg, a has weight 1, z has weight 26.
Input:
The first line of the input contains a single integer T, denoting the number of test cases. Then T test cases follow. Each testcase has 2 lines
The first string S1
The second string S2
Output:
Print 1 if the weight of the first string is greater. Print 2 if the weight of the second string is greater. Print equal if the the weights are equal.
Constraints:
1<=T<=100
1<=|S1|<=1000
1<=|S2|<=1000
Example:
Input:
4
batman
superman
kira
l
goku
broly
manbat
batman
Output:
2
1
2
equal
Note: The strings contains only lowercase characters.
0
rutwikgosavi3 months ago
t = int(input())
for i in range(t):
    s1 = input()
    s2 = input()
    l1 = l2 = 0
    for x in s1:
        l1 += (ord(x)%96)
    for y in s2:
        l2 += (ord(y)%96)
    if l1>l2:
        print(1)
    elif l1<l2:
        print(2)
    else:
        print('equal')
+1
tusharkhanna51124 months ago
#code by Tushar Khanna
for _ in range(int(input())):
    s1=str(input())
    s2=str(input())
    l1=l2=0
    for i in s1:
        l1+=(ord(i)%96)
    for j in s2:
        l2+=(ord(j)%96)
    if(l1>l2):
        print(1)
    if(l1<l2):
        print(2)
    if(l1==l2):
        print('equal')
        
python code
0
rohitpendse1386 months ago
#include <bits/stdc++.h>

#define int long long
using namespace std;

void solve(string &s1, string &s2) {
    int w1 = 0, w2 = 0;
    for (char &c: s1) w1 += c - 'a' + 1;
    for (char &c: s2) w2 += c - 'a' + 1;
    if (w1 == w2) cout << "equal\n";
    else if (w1 > w2) cout << "1\n";
    else cout << "2\n";
}

int32_t main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        string s1, s2;
        cin >> s1;
        cin >> s2;
        solve(s1, s2);
    }
    return 0;
}
0
sushantkumarnast7 months ago
// Please Help me 
// input of yagami and light gives output 1 but correct output is equal 
// how???
// how???
Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for(int i=1; i<=T; i++){//       input     String s1= sc.next();     String s2 = sc.next();     int sum1 =0;     int sum2 =0;//          Making char arrays     char a[] = s1.toLowerCase().toCharArray();     char b[] = s2.toLowerCase().toCharArray();//          Loops for sum                for(int j=0; j<s1.length();j++){         sum1 = sum1 + a[j];     }     for(int j=0; j<s2.length();j++){         sum2 = sum2 + b[j];     }//          Checker                     if(sum1>sum2){               System.out.println("1");           }else if(sum1<sum2){               System.out.println("2");           }else{               System.out.print("equal");           }
0
ybrijesh7 months ago
#include<bits/stdc++.h>#include<string>using namespace std;int main(){//codeint t;cin>>t;while(t--){        string s1;    string s2;    cin>>s1;    cin>>s2;    int sum=0;    int sum1=0;        for(int i=0;i<s1.length();i++)    {        sum=sum+(int)s1[i]-96;    }        for(int i=0;i<s2.length();i++)    {        sum1=sum1+(int)s2[i]-96;    }       if(sum>sum1)   {       cout<<"1"<<endl;   }   else if(sum1>sum)   {       cout<<"2"<<endl;   }   else if(sum==sum1)   {       cout<<"equal"<<endl;   }            }return 0;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.