Check if Word Equals Summation of Two Words

šŸ  ā¬…ļø āž”ļø

The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, etc.).

The numerical value of some string of lowercase English letters s is the concatenation of the letter values of each letter in s, which is then converted into an integer.

  • For example, if s = "acb ", we concatenate each letter's letter value, resulting in "021 ". After converting it, we get 21.

You are given three strings firstWord, secondWord, and targetWord, each consisting of lowercase English letters 'a' through 'j' inclusive.

Return true if the summation of the numerical values of firstWord and secondWord equals the numerical value of targetWord, or false otherwise.

Example 1:

Input: firstWord = "acb ", secondWord = "cba ", targetWord = "cdb " Output: true Explanation: The numerical value of firstWord is "acb " -> "021 " -> 21. The numerical value of secondWord is "cba " -> "210 " -> 210. The numerical value of targetWord is "cdb " -> "231 " -> 231. We return true because 21 + 210 == 231.

Example 2:

Input: firstWord = "aaa ", secondWord = "a ", targetWord = "aab " Output: false Explanation: The numerical value of firstWord is "aaa " -> "000 " -> 0. The numerical value of secondWord is "a " -> "0 " -> 0. The numerical value of targetWord is "aab " -> "001 " -> 1. We return false because 0 + 0 != 1.

Example 3:

Input: firstWord = "aaa ", secondWord = "a ", targetWord = "aaaa " Output: true Explanation: The numerical value of firstWord is "aaa " -> "000 " -> 0. The numerical value of secondWord is "a " -> "0 " -> 0. The numerical value of targetWord is "aaaa " -> "0000 " -> 0. We return true because 0 + 0 == 0.

Constraints:

  • 1 <= firstWord.length, secondWord.length, targetWord.length <= 8
  • firstWord, secondWord, and targetWord consist of lowercase English letters from 'a' to 'j' inclusive.

Note: This problem is from LeetCode.
āœ… Compiled
āœ… Executed
āš ļø Correct
program main
    implicit none

    character(len=8) :: firstWord, secondWord, targetWord
    logical :: result

    ! Example 1
    firstWord = 'acb '
    secondWord = 'cba '
    targetWord = 'cdb '
    result = checkSum(firstWord, secondWord, targetWord)
    write(*,*) result

    ! Example 2
    firstWord = 'aaa '
    secondWord = 'a '
    targetWord = 'aab '
    result = checkSum(firstWord, secondWord, targetWord)
    write(*,*) result

    ! Example 3
    firstWord = 'aaa '
    secondWord = 'a '
    targetWord = 'aaaa '
    result = checkSum(firstWord, secondWord, targetWord)
    write(*,*) result

contains

    function checkSum(firstWord, secondWord, targetWord) result(result_)
        implicit none
        character(len=*), intent(in) :: firstWord, secondWord, targetWord
        logical :: result_
        integer :: firstWordNumericalValue, secondWordNumericalValue, targetWordNumericalValue

        firstWordNumericalValue = numericalValue(firstWord)
        secondWordNumericalValue = numericalValue(secondWord)
        targetWordNumericalValue = numericalValue(targetWord)

        result_ = firstWordNumericalValue + secondWordNumericalValue == targetWordNumericalValue
    end function checkSum

    function numericalValue(string) result(value)
        implicit none
        character(len=*), intent(in) :: string
        integer :: value
        integer :: i

        value = 0
        do i = 1, len(string)
            value = value * 10 + ichar(string(i:i)) - ichar('a')
        end do
    end function numericalValue
end program main
āŒ Compiled
āŒ Executed
āŒ Correct
PROGRAM main

IMPLICIT NONE

CHARACTER(LEN=8) :: firstWord, secondWord, targetWord
INTEGER :: firstWordValue, secondWordValue, targetWordValue

! Read input
READ(*,*) firstWord, secondWord, targetWord

! Calculate the numerical value of each word
firstWordValue = 0
DO i = 1, LEN(firstWord)
    firstWordValue = firstWordValue * 10 + ICHAR(firstWord(i:i)) - ICHAR('a')
END DO

secondWordValue = 0
DO i = 1, LEN(secondWord)
    secondWordValue = secondWordValue * 10 + ICHAR(secondWord(i:i)) - ICHAR('a')
END DO

targetWordValue = 0
DO i = 1, LEN(targetWord)
    targetWordValue = targetWordValue * 10 + ICHAR(targetWord(i:i)) - ICHAR('a')
END DO

! Check if the sum of the numerical values of the first two words equals the numerical value of the third word
IF (firstWordValue + secondWordValue == targetWordValue) THEN
    PRINT *, "True"
ELSE
    PRINT *, "False"
END IF

END PROGRAM main
šŸŒ Data from online sources
def largest_merge(word1: str, word2: str) -> str:
    merge = []
    while word1 or word2:
        if word1 > word2:
            merge.append(word1[0])
            word1 = word1[1:]
        else:
            merge.append(word2[0])
            word2 = word2[1:]
    return ''.join(merge)

The algorithm starts with two given strings, word1 and word2. The goal is to create the lexicographically largest merge string by choosing characters from either of the input strings. The algorithm operates in a while loop that continues until both input strings are empty.

In each iteration of the loop, the algorithm compares the input strings lexicographically. If word1 is lexicographically larger, it takes the first character of word1, appends it to the merge string, and removes it from the input string. If word2 is lexicographically larger or equal, it does the same operation for word2.

This process continues until both input strings are empty, resulting in the lexicographically largest possible merge string. Then the merge string is returned.

šŸŒ Data from online sources
#include <string>

std::string largestMerge(std::string word1, std::string word2) {
    std::string merge = "";
    while (!word1.empty() || !word2.empty()) {
        if (word1 > word2) {
            merge += word1[0];
            word1 = word1.substr(1);
        } else {
            merge += word2[0];
            word2 = word2.substr(1);
        }
    }
    return merge;
}

The algorithm starts with two given strings, word1 and word2. The goal is to create the lexicographically largest merge string by choosing characters from either of the input strings. The algorithm operates in a while loop that continues until both input strings are empty.

In each iteration of the loop, the algorithm compares the input strings lexicographically. If word1 is lexicographically larger, it takes the first character of word1, appends it to the merge string, and removes it from the input string. If word2 is lexicographically larger or equal, it does the same operation for word2.

This process continues until both input strings are empty, resulting in the lexicographically largest possible merge string. Then the merge string is returned.