检查给定字符串的字母顺序总和是否相等
给定两个字符串s1和s2 ,任务是检查两个给定字符的字母顺序和字符串相等
例子:
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
方法:该任务可以通过简单地遍历字符串来找到字符的字母顺序总和来解决。对于每个字符,在结果答案中添加当前字符的 ASCII 值 + 1 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出:
True
时间复杂度: O(N)
辅助空间: O(1)