Given two strings ransomNote
and magazine
, return true
if ransomNote
can be constructed by using the letters from magazine
and false
otherwise.
Each letter in magazine
can only be used once in ransomNote
.
Example 1:
Input: ransomNote = "a", magazine = "b" Output: false
Example 2:
Input: ransomNote = "aa", magazine = "ab" Output: false
Example 3:
Input: ransomNote = "aa", magazine = "aab" Output: true
Constraints:
1 <= ransomNote.length, magazine.length <= 105
ransomNote
and magazine
consist of lowercase English letters.program main
implicit none
character(len=:), allocatable :: ransomNote, magazine
logical :: result
! Example 1
allocate(character(len=1) :: ransomNote)
allocate(character(len=1) :: magazine)
ransomNote = 'a'
magazine = 'b'
result = canConstruct(ransomNote, magazine)
print *, result
deallocate(ransomNote)
deallocate(magazine)
! Example 2
allocate(character(len=2) :: ransomNote)
allocate(character(len=2) :: magazine)
ransomNote = 'aa'
magazine = 'ab'
result = canConstruct(ransomNote, magazine)
print *, result
deallocate(ransomNote)
deallocate(magazine)
! Example 3
allocate(character(len=2) :: ransomNote)
allocate(character(len=3) :: magazine)
ransomNote = 'aa'
magazine = 'aab'
result = canConstruct(ransomNote, magazine)
print *, result
deallocate(ransomNote)
deallocate(magazine)
contains
function canConstruct(ransomNote, magazine) result(result_)
implicit none
character(len=*), intent(in) :: ransomNote, magazine
integer :: i, j
logical :: result_
do i = 1, len(ransomNote)
do j = 1, len(magazine)
if (ransomNote(i:i) == magazine(j:j)) then
magazine(j:j) = ' '
exit
end if
end do
if (j > len(magazine)) then
result_ = .false.
return
end if
end do
result_ = .true.
end function canConstruct
end program main
temp.f95:48:20: 48 | magazine(j:j) = ' ' | 1 Error: Dummy argument ‘magazine’ with INTENT(IN) in variable definition context (assignment) at (1)
module main
implicit none
interface
function canConstruct(ransomNote, magazine) result(output)
implicit none
character(len=*), intent(in) :: ransomNote, magazine
logical :: output
end function canConstruct
end interface
! Test cases
character(len=100) :: ransomNote, magazine
logical :: output
! Test case 1
ransomNote = "a"
magazine = "b"
output = canConstruct(ransomNote, magazine)
if (output) then
write (*,*) "Test case 1: FAILED"
else
write (*,*) "Test case 1: PASSED"
end if
! Test case 2
ransomNote = "aa"
magazine = "ab"
output = canConstruct(ransomNote, magazine)
if (output) then
write (*,*) "Test case 2: FAILED"
else
write (*,*) "Test case 2: PASSED"
end if
! Test case 3
ransomNote = "aa"
magazine = "aab"
output = canConstruct(ransomNote, magazine)
if (output) then
write (*,*) "Test case 3: PASSED"
else
write (*,*) "Test case 3: FAILED"
end if
contains
function canConstruct(ransomNote, magazine) result(output)
implicit none
character(len=*), intent(in) :: ransomNote, magazine
integer :: i, j
logical :: output
! Initialize the output variable
output = .false.
! Loop through each character in ransomNote
do i = 1, len(ransomNote)
! Loop through each character in magazine
do j = 1, len(magazine)
! If the current character in ransomNote matches the current character in magazine
if (ransomNote(i:i) == magazine(j:j)) then
! If the current character in magazine is the last character in magazine
if (j == len(magazine)) then
! Set the output variable to true
output = .true.
! Exit the loop
exit
! Otherwise, continue looping
else
cycle
end if
end if
end do
! If the output variable is still false, then the current character in ransomNote cannot be constructed
if (.not. output) then
exit
end if
end do
end function canConstruct
end program main
temp.f95:20:16: 20 | ransomNote = "a" | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:21:14: 21 | magazine = "b" | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:22:43: 22 | output = canConstruct(ransomNote, magazine) | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:23:16: 23 | if (output) then | 1 Error: Unexpected block IF statement in MODULE at (1) temp.f95:24:37: 24 | write (*,*) "Test case 1: FAILED" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:25:4: 25 | else | 1 Error: Unexpected ELSE statement in MODULE at (1) temp.f95:26:37: 26 | write (*,*) "Test case 1: PASSED" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:27:3: 27 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:30:17: 30 | ransomNote = "aa" | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:31:15: 31 | magazine = "ab" | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:32:43: 32 | output = canConstruct(ransomNote, magazine) | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:33:16: 33 | if (output) then | 1 Error: Unexpected block IF statement in MODULE at (1) temp.f95:34:37: 34 | write (*,*) "Test case 2: FAILED" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:35:4: 35 | else | 1 Error: Unexpected ELSE statement in MODULE at (1) temp.f95:36:37: 36 | write (*,*) "Test case 2: PASSED" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:37:3: 37 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:40:17: 40 | ransomNote = "aa" | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:41:16: 41 | magazine = "aab" | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:42:43: 42 | output = canConstruct(ransomNote, magazine) | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:43:16: 43 | if (output) then | 1 Error: Unexpected block IF statement in MODULE at (1) temp.f95:44:37: 44 | write (*,*) "Test case 3: PASSED" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:45:4: 45 | else | 1 Error: Unexpected ELSE statement in MODULE at (1) temp.f95:46:37: 46 | write (*,*) "Test case 3: FAILED" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:47:3: 47 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:51:22: 7 | function canConstruct(ransomNote, magazine) result(output) | 2 ...... 51 | function canConstruct(ransomNote, magazine) result(output) | 1 Error: Procedure ‘canconstruct’ at (1) is already defined at (2) temp.f95:52:17: 52 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:53:56: 53 | character(len=*), intent(in) :: ransomNote, magazine | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:54:19: 54 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:55:21: 55 | logical :: output | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:58:20: 58 | output = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:61:29: 61 | do i = 1, len(ransomNote) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:63:31: 63 | do j = 1, len(magazine) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:65:54: 65 | if (ransomNote(i:i) == magazine(j:j)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:67:44: 67 | if (j == len(magazine)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:69:35: 69 | output = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:71:24: 71 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:73:20: 73 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:74:25: 74 | cycle | 1 Error: CYCLE statement at (1) is not within a construct temp.f95:75:19: 75 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:76:15: 76 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:77:11: 77 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:79:30: 79 | if (.not. output) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:80:16: 80 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:81:11: 81 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:82:7: 82 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:84:3: 84 | end function canConstruct | 1 Error: Expecting END MODULE statement at (1) temp.f95:86:3: 86 | end program main | 1 Error: Expecting END MODULE statement at (1) f951: Error: Unexpected end of file in ‘temp.f95’
def can_construct(ransom_note, magazine):
letter_count = {}
for c in magazine:
letter_count[c] = letter_count.get(c, 0) + 1
for c in ransom_note:
if c not in letter_count or letter_count[c] == 0:
return False
letter_count[c] -= 1
return True
The algorithm works as follows:
1. Create a hash map called letterCount
, which holds the count of each character in the magazine
string.
2. Iterate over the characters in ransomNote
. For each character:
a. If the character is not in letterCount
or its count is 0, return false
, as the required letter is not available in magazine
.
b. Decrease the count of the character in letterCount
by 1.
3. Once the iteration is done, if the entire ransomNote
string is processed without any issue, return true
.
This algorithm checks if the ransomNote
can be made from the available characters in the magazine
by using a single pass through each string and maintaining the count of characters in a hash map.
#include <string>
#include <unordered_map>
bool canConstruct(std::string ransomNote, std::string magazine) {
std::unordered_map<char, int> letterCount;
for (char c : magazine) {
letterCount[c]++;
}
for (char c : ransomNote) {
if (letterCount[c] == 0) {
return false;
}
letterCount[c]--;
}
return true;
}
The algorithm works as follows:
1. Create a hash map called letterCount
, which holds the count of each character in the magazine
string.
2. Iterate over the characters in ransomNote
. For each character:
a. If the character is not in letterCount
or its count is 0, return false
, as the required letter is not available in magazine
.
b. Decrease the count of the character in letterCount
by 1.
3. Once the iteration is done, if the entire ransomNote
string is processed without any issue, return true
.
This algorithm checks if the ransomNote
can be made from the available characters in the magazine
by using a single pass through each string and maintaining the count of characters in a hash map.