Find the Difference

🏠 ⬅️ ➡️

You are given two strings s and t.

String t is generated by random shuffling string s and then add one more letter at a random position.

Return the letter that was added to t.

Example 1:

Input: s = "abcd ", t = "abcde " Output: "e " Explanation: 'e' is the letter that was added.

Example 2:

Input: s = " ", t = "y " Output: "y "

Constraints:

  • 0 <= s.length <= 1000
  • t.length == s.length + 1
  • s and t consist of lowercase English letters.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    character(len=*), parameter :: s = "abcd "
    character(len=*), parameter :: t = "abcde "
    character(len=1) :: ans

    ans = solve(s, t)
    print *, ans

contains

    function solve(s, t) result(ans)
        implicit none
        character(len=*), intent(in) :: s
        character(len=*), intent(in) :: t
        integer :: i, j
        character(len=1) :: ans

        do i = 1, len_trim(s)
            if (s(i:i) /= t(i:i)) then
                ans = t(i:i)
                return
            end if
        end do

        ans = ' '
    end function solve
end program main
Compiled
Executed
Correct
module euler_problem_17

implicit none

contains

function get_random_letter(length) result(letter)

integer, intent(in) :: length
character(len=1) :: letter

letter = char(iachar('a') + random_integer(length=1) - 1)

end function get_random_letter

function get_random_position(length) result(position)

integer, intent(in) :: length
integer :: position

position = random_integer(length=1)

end function get_random_position

function solve(s, t) result(letter)

character(len=*), intent(in) :: s
character(len=*), intent(in) :: t
character(len=1) :: letter

letter = t(len_trim(s) + 1:len_trim(s) + 1)

end function solve

end module euler_problem_17

program main

use euler_problem_17

implicit none

character(len=1000) :: s
character(len=1000) :: t
character(len=1) :: letter

s = "abcd "
t = "abcde "

letter = solve(s, t)

write (*,*) "The letter that was added to t is: ", letter

s = " "
t = "y "

letter = solve(s, t)

write (*,*) "The letter that was added to t is: ", letter

end program main
🌐 Data from online sources
def findTheDifference(s: str, t: str) -> str:
    return chr(sum(ord(c) for c in t) - sum(ord(c) for c in s))

The algorithm calculates the sum of the ASCII values of characters in both strings s and t. Since t is a shuffled version of s plus an extra character, the difference in the sums of ASCII values will give us the ASCII value of the added character. Finally, we return the character corresponding to this ASCII value.

🌐 Data from online sources
char findTheDifference(std::string s, std::string t) {
    int sum_s = 0, sum_t = 0;
    for(char c : s) sum_s += c;
    for(char c : t) sum_t += c;
    return sum_t - sum_s;
}

The algorithm calculates the sum of the ASCII values of characters in both strings s and t. Since t is a shuffled version of s plus an extra character, the difference in the sums of ASCII values will give us the ASCII value of the added character. Finally, we return the character corresponding to this ASCII value.