You are given the strings key and message, which represent a cipher key and a secret message, respectively. The steps to decode message are as follows:
key as the order of the substitution table.message is then substituted using the table.Spaces ' ' are transformed to themselves.
For example, given key = "**hap**p**y** **bo**y " (actual key would have at least one instance of each letter in the alphabet), we have the partial substitution table of ('h' -> 'a', 'a' -> 'b', 'p' -> 'c', 'y' -> 'd', 'b' -> 'e', 'o' -> 'f').
Return the decoded message.
Example 1:
Input: key = "the quick brown fox jumps over the lazy dog ", message = "vkbs bs t suepuv " Output: "this is a secret " Explanation: The diagram above shows the substitution table. It is obtained by taking the first appearance of each letter in "the quick brown fox jumps over the lazy dog ".
Example 2:
Input: key = "eljuxhpwnyrdgtqkviszcfmabo ", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb " Output: "the five boxing wizards jump quickly " Explanation: The diagram above shows the substitution table. It is obtained by taking the first appearance of each letter in "eljuxhpwnyrdgtqkviszcfmabo ".
Constraints:
26 <= key.length <= 2000key consists of lowercase English letters and ' '.key contains every letter in the English alphabet ('a' to 'z') at least once.1 <= message.length <= 2000message consists of lowercase English letters and ' '.program main
implicit none
character(len=*), parameter :: key = "**hap**p** **bo**y "
character(len=*), parameter :: message = "vkbs bs t suepuv "
character(len=:), allocatable :: decoded_message
call decode_message(key, message, decoded_message)
write (*,*) decoded_message
contains
subroutine decode_message(key, message, decoded_message)
implicit none
character(len=*), intent(in) :: key
character(len=*), intent(in) :: message
character(len=:), allocatable, intent(out) :: decoded_message
integer :: i, j, k
character(len=1), dimension(26) :: order
character(len=1), dimension(26) :: alphabet
character(len=1), dimension(:), allocatable :: substitution_table
! Initialize the substitution table
do i = 1, 26
order(i) = key(i:i)
alphabet(i) = char(97 + i - 1)
end do
! Create the substitution table
allocate(substitution_table(26))
do i = 1, 26
j = index(alphabet, order(i))
substitution_table(j) = alphabet(i)
end do
! Substitute each letter in the message
allocate(character(len=len(message)) :: decoded_message)
do i = 1, len(message)
if (message(i:i) == ' ') then
decoded_message(i:i) = ' '
else
k = index(alphabet, message(i:i))
decoded_message(i:i) = substitution_table(k)
end if
end do
end subroutine decode_message
end program main
temp.f95:33:8:
33 | j = index(alphabet, order(i))
| 1
Error: Incompatible ranks 0 and 1 in assignment at (1)
temp.f95:43:12:
43 | k = index(alphabet, message(i:i))
| 1
Error: Incompatible ranks 0 and 1 in assignment at (1)
! Decrypts a message using a substitution cipher based on a given key.
!
! Input:
! - key: a string containing the cipher key
! - message: a string containing the encrypted message
!
! Output:
! - the decrypted message
program decrypt
implicit none
character(len=*), parameter :: alphabet = "abcdefghijklmnopqrstuvwxyz"
character(len=*), parameter :: space = " "
character(len=*) :: key, message
character(len=*) :: decrypted_message
integer :: i, j, k, l
! Initialize the substitution table
character(len=1), dimension(26) :: substitution_table
do i = 1, 26
substitution_table(i) = alphabet(i:i)
end do
! Align the substitution table with the regular English alphabet
do i = 1, 26
j = 1
do while (substitution_table(i) /= alphabet(j:j))
j = j + 1
end do
substitution_table(i) = alphabet(j:j)
end do
! Decode the message
decrypted_message = ""
do i = 1, len(message)
if (message(i:i) == space) then
decrypted_message = decrypted_message // space
else
k = 1
do while (substitution_table(k) /= message(i:i))
k = k + 1
end do
decrypted_message = decrypted_message // substitution_table(k)
end if
end do
! Print the decrypted message
print *, decrypted_message
end program decrypt
temp.f95:17:39:
17 | character(len=*) :: decrypted_message
| 1
Error: Entity with assumed character length at (1) must be a dummy argument or a PARAMETER
temp.f95:16:25:
16 | character(len=*) :: key, message
| 1
Error: Entity with assumed character length at (1) must be a dummy argument or a PARAMETER
temp.f95:16:34:
16 | character(len=*) :: key, message
| 1
Error: Entity with assumed character length at (1) must be a dummy argument or a PARAMETER
def valid_selections(s: str) -> int:
res = 0
for i in range(1, len(s) - 1):
if s[i - 1] != s[i] and s[i] != s[i + 1] and s[i - 1] != s[i + 1]:
res += 1
return res
We iterate through the binary string s starting from index 1 till the second-last character in the string. For each character s[i], we check three conditions:
1. s[i-1] should be different from s[i]
2. s[i] should be different from s[i+1]
3. s[i+1] should be different from s[i-1]
If all these conditions are met for the current index i, then we can increment the result counter. Finally, we return the result counter, which is the number of valid ways to select 3 buildings.
#include <string>
int validSelections(const std::string& s) {
int res = 0;
for (int i = 1; i < s.size() - 1; ++i) {
if (s[i - 1] != s[i] && s[i] != s[i + 1] && s[i + 1] != s[i - 1]) {
res++;
}
}
return res;
}
We iterate through the binary string s starting from index 1 till the second-last character in the string. For each character s[i], we check three conditions:
1. s[i-1] should be different from s[i]
2. s[i] should be different from s[i+1]
3. s[i+1] should be different from s[i-1]
If all these conditions are met for the current index i, then we can increment the result counter. Finally, we return the result counter, which is the number of valid ways to select 3 buildings.