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.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
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
temp.f95:21:33: 21 | position = random_integer(length=1) | 1 Error: Keyword argument requires explicit interface for procedure ‘random_integer’ at (1) temp.f95:21:11: 21 | position = random_integer(length=1) | 1 Error: Function ‘random_integer’ at (1) has no IMPLICIT type temp.f95:12:50: 12 | letter = char(iachar('a') + random_integer(length=1) - 1) | 1 Error: Keyword argument requires explicit interface for procedure ‘random_integer’ at (1) temp.f95:12:42: 12 | letter = char(iachar('a') + random_integer(length=1) - 1) | 1 Error: Function ‘random_integer’ at (1) has no IMPLICIT type; did you mean ‘get_random_letter’? temp.f95:39:5: 39 | use euler_problem_17 | 1 Fatal Error: Cannot open module file ‘euler_problem_17.mod’ for reading at (1): No such file or directory compilation terminated.
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.
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.