📌  相关文章
📜  检查两个词的总和是否等于目标词

📅  最后修改于: 2022-05-13 01:57:07.965000             🧑  作者: Mango

检查两个词的总和是否等于目标词

给定三个大小分别为 L、M 和 N 的字符串ABC ,并且仅由小于“K”的小写英文字母组成。任务是检查字符串AB的总和是否等于字符串C ,在将字符串解码为整数后,通过将字母表与其在字母表中的索引值进行映射并将它们连接起来。

例子:

方法:可以使用类似的方法来解决问题,该方法用于查找表示为字符串的两个大数之和。请按照以下步骤解决问题:

  • 反转字符串ABC
  • 初始化两个变量,比如currrem0以存储第i位置的值以及字符串AB之和的余数。
  • 使用变量i迭代范围[0, max(L, max(M, N))]并执行以下步骤:
    • 将字符串AB第 i索引处的字符总和存储在变量curr中。
    • curr更新为curr = curr+rem ,然后将rem更新为rem = curr/10。
    • 现在检查i是否小于N并且curr%10不等于C[i]-'a'即字符串C的第i字符处的值,然后打印“ No ”并返回。
  • 最后,完成上述步骤后,如果rem大于0则打印“ No ”。否则,打印“”。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check weather summation
// of two words equal to target word
string isSumEqual(string A, string B, string C)
{
 
    // Store the length of each string
    int L = A.length();
    int M = B.length();
    int N = A.length();
 
    // Reverse the strings A, B and C
    reverse(A.begin(), A.end());
    reverse(B.begin(), B.end());
    reverse(C.begin(), C.end());
 
    // Stores the remainder
    int rem = 0;
 
    // Iterate in the range
    // [0, max(L, max(M, N))]
    for (int i = 0; i < max(L, max(M, N)); i++) {
 
        // Stores the integer at ith
        // position from the right in
        // the sum of A and B
        int curr = rem;
 
        // If i is less than L
        if (i < L)
            curr += A[i] - 'a';
 
        // If i is less than M
        if (i < M)
            curr += B[i] - 'a';
 
        // Update rem and curr
        rem = curr / 10;
        curr %= 10;
 
        // If i is less than N
        // and curr is not equal
        // to C[i]-'a', return "No"
        if (i < N && curr != C[i] - 'a') {
            return "No";
        }
    }
 
    // If rem is greater
    // than 0, return "No"
    if (rem)
        return "No";
 
    // Otherwise, return "Yes"
    else
        return "Yes";
}
 
// Driver Code
int main()
{
 
    // Given Input
    string A = "acb", B = "cba", C = "cdb";
 
    // Function Call
    cout << isSumEqual(A, B, C);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check weather summation
// of two words equal to target word
static String isSumEqual(String A, String B, String C)
{
 
    // Store the length of each String
    int L = A.length();
    int M = B.length();
    int N = A.length();
 
    // Reverse the Strings A, B and C
    A = reverse(A);
    B = reverse(B);
    C = reverse(C);
 
    // Stores the remainder
    int rem = 0;
 
    // Iterate in the range
    // [0, Math.max(L, Math.max(M, N))]
    for (int i = 0; i < Math.max(L, Math.max(M, N)); i++) {
 
        // Stores the integer at ith
        // position from the right in
        // the sum of A and B
        int curr = rem;
 
        // If i is less than L
        if (i < L)
            curr += A.charAt(i) - 'a';
 
        // If i is less than M
        if (i < M)
            curr += B.charAt(i) - 'a';
 
        // Update rem and curr
        rem = curr / 10;
        curr %= 10;
 
        // If i is less than N
        // and curr is not equal
        // to C[i]-'a', return "No"
        if (i < N && curr != C.charAt(i) - 'a') {
            return "No";
        }
    }
 
    // If rem is greater
    // than 0, return "No"
    if (rem>0)
        return "No";
 
    // Otherwise, return "Yes"
    else
        return "Yes";
}
static String reverse(String input) {
    char[] a = input.toCharArray();
    int l, r = a.length - 1;
    for (l = 0; l < r; l++, r--) {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.valueOf(a);
}
   
// Driver Code
public static void main(String[] args)
{
 
    // Given Input
    String A = "acb", B = "cba", C = "cdb";
 
    // Function Call
    System.out.print(isSumEqual(A, B, C));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to check weather summation
# of two words equal to target word
def isSumEqual(A, B, C):
 
    # Store the length of each string
    L = len(A)
    M = len(B)
    N = len(A)
 
    # Reverse the strings A, B and C
    A = A[::-1]
    B = B[::-1]
    C = C[::-1]
 
    # Stores the remainder
    rem = 0
 
    # Iterate in the range
    # [0, max(L, max(M, N))]
    for i in range(max(L, max(M, N))):
 
        # Stores the integer at ith
        # position from the right in
        # the sum of A and B
        curr = rem
 
        # If i is less than L
        if (i < L):
            curr += ord(A[i]) - ord('a')
 
        # If i is less than M
        if (i < M):
            curr += ord(B[i]) - ord('a')
 
        # Update rem and curr
        rem = curr // 10
        curr %= 10
 
        # If i is less than N
        # and curr is not equal
        # to C[i]-'a', return "No"
        if (i < N and curr != ord(C[i]) - ord('a')):
            return "No"
 
    # If rem is greater
    # than 0, return "No"
    if (rem):
        return "No"
       
    # Otherwise, return "Yes"
    else:
        return "Yes"
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    A = "acb"
    B = "cba"
    C = "cdb"
 
    # Function Call
    print (isSumEqual(A, B, C))
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
public class GFG{
 
// Function to check weather summation
// of two words equal to target word
static String isSumEqual(String A, String B, String C)
{
 
    // Store the length of each String
    int L = A.Length;
    int M = B.Length;
    int N = A.Length;
 
    // Reverse the Strings A, B and C
    A = reverse(A);
    B = reverse(B);
    C = reverse(C);
 
    // Stores the remainder
    int rem = 0;
 
    // Iterate in the range
    // [0, Math.Max(L, Math.Max(M, N))]
    for (int i = 0; i < Math.Max(L, Math.Max(M, N)); i++) {
 
        // Stores the integer at ith
        // position from the right in
        // the sum of A and B
        int curr = rem;
 
        // If i is less than L
        if (i < L)
            curr += A[i] - 'a';
 
        // If i is less than M
        if (i < M)
            curr += B[i] - 'a';
 
        // Update rem and curr
        rem = curr / 10;
        curr %= 10;
 
        // If i is less than N
        // and curr is not equal
        // to C[i]-'a', return "No"
        if (i < N && curr != C[i] - 'a') {
            return "No";
        }
    }
 
    // If rem is greater
    // than 0, return "No"
    if (rem>0)
        return "No";
 
    // Otherwise, return "Yes"
    else
        return "Yes";
}
static String reverse(String input) {
    char[] a = input.ToCharArray();
    int l, r = a.Length - 1;
    for (l = 0; l < r; l++, r--) {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.Join("",a);
}
   
// Driver Code
public static void Main(String[] args)
{
 
    // Given Input
    String A = "acb", B = "cba", C = "cdb";
 
    // Function Call
    Console.Write(isSumEqual(A, B, C));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出
Yes

时间复杂度: O(L+M+N)
辅助空间: O(1)