We can represent a sentence as an array of words, for example, the sentence "I am happy with leetcode "
can be represented as arr = [ "I ", "am ",happy ", "with ", "leetcode "]
.
Given two sentences sentence1
and sentence2
each represented as a string array and given an array of string pairs similarPairs
where similarPairs[i] = [xi, yi]
indicates that the two words xi
and yi
are similar.
Return true
if sentence1
and sentence2
are similar, or false
if they are not similar.
Two sentences are similar if:
sentence1[i]
and sentence2[i]
are similar.Notice that a word is always similar to itself, also notice that the similarity relation is not transitive. For example, if the words a
and b
are similar, and the words b
and c
are similar, a
and c
are not necessarily similar.
Example 1:
Input: sentence1 = [ "great ", "acting ", "skills "], sentence2 = [ "fine ", "drama ", "talent "], similarPairs = [[ "great ", "fine "],[ "drama ", "acting "],[ "skills ", "talent "]] Output: true Explanation: The two sentences have the same length and each word i of sentence1 is also similar to the corresponding word in sentence2.
Example 2:
Input: sentence1 = [ "great "], sentence2 = [ "great "], similarPairs = [] Output: true Explanation: A word is similar to itself.
Example 3:
Input: sentence1 = [ "great "], sentence2 = [ "doubleplus ", "good "], similarPairs = [[ "great ", "doubleplus "]] Output: false Explanation: As they don't have the same length, we return false.
Constraints:
1 <= sentence1.length, sentence2.length <= 1000
1 <= sentence1[i].length, sentence2[i].length <= 20
sentence1[i]
and sentence2[i]
consist of English letters.0 <= similarPairs.length <= 1000
similarPairs[i].length == 2
1 <= xi.length, yi.length <= 20
xi
and yi
consist of lower-case and upper-case English letters.(xi, yi)
are distinct.program main
implicit none
integer :: i, j, n, m
character(len=20), allocatable :: arr(:)
character(len=20), allocatable :: sentence1(:), sentence2(:)
character(len=20), allocatable :: xi(:), yi(:)
logical :: result
! Examples
n = 5
allocate(arr(n))
arr = ["I ", "am ", "happy ", "with ", "leetcode "]
call print_array(arr)
n = 4
allocate(sentence1(n))
sentence1 = ["great ", "acting ", "skills "]
allocate(sentence2(n))
sentence2 = ["fine ", "drama ", "talent "]
allocate(xi(2))
allocate(yi(2))
xi = ["great ", "drama "]
yi = ["fine ", "acting "]
result = are_sentences_similar(sentence1, sentence2, xi, yi)
write (*, '(A, L1)') "Example 1:", result
n = 1
allocate(sentence1(n))
sentence1 = ["great "]
allocate(sentence2(n))
sentence2 = ["great "]
allocate(xi(0))
allocate(yi(0))
result = are_sentences_similar(sentence1, sentence2, xi, yi)
write (*, '(A, L1)') "Example 2:", result
n = 1
allocate(sentence1(n))
sentence1 = ["great "]
allocate(sentence2(n))
sentence2 = ["doubleplus ", "good "]
allocate(xi(1))
allocate(yi(1))
xi = ["great "]
yi = ["doubleplus "]
result = are_sentences_similar(sentence1, sentence2, xi, yi)
write (*, '(A, L1)') "Example 3:", result
contains
subroutine print_array(arr)
implicit none
character(len=20), intent(in) :: arr(:)
do i = 1, size(arr)
write (*, '(A)', advance='no') trim(arr(i)) // " "
end do
write (*, *) ""
end subroutine print_array
end program main
function are_sentences_similar(sentence1, sentence2, similarPairs) result(result)
implicit none
character(len=20), intent(in) :: sentence1(:), sentence2(:)
character(len=20), intent(in) :: similarPairs(:, :)
logical :: result
integer :: i, j, k, l
! Check if both sentences have the same length
if (size(sentence1) /= size(sentence2)) then
result = .false.
return
end if
! Check if each word in sentence1 is similar to the corresponding word in sentence2
do i = 1, size(sentence1)
do j = 1, size(similarPairs, dim=1)
if (sentence1(i) == similarPairs(j, 1) .and. &
sentence2(i) == similarPairs(j, 2)) then
exit
end if
end do
if (j > size(similarPairs, dim=1)) then
result = .false.
return
end if
end do
result = .true.
end function are_sentences_similar
temp.f95:13:16: 13 | arr = ["I ", "am ", "happy ", "with ", "leetcode "] | 1 Error: Different CHARACTER lengths (2/3) in array constructor at (1) temp.f95:18:26: 18 | sentence1 = ["great ", "acting ", "skills "] | 1 Error: Different CHARACTER lengths (6/7) in array constructor at (1) temp.f95:20:25: 20 | sentence2 = ["fine ", "drama ", "talent "] | 1 Error: Different CHARACTER lengths (5/6) in array constructor at (1) temp.f95:24:18: 24 | yi = ["fine ", "acting "] | 1 Error: Different CHARACTER lengths (5/7) in array constructor at (1) temp.f95:25:34: 25 | result = are_sentences_similar(sentence1, sentence2, xi, yi) | 1 Error: Return type mismatch of function āare_sentences_similarā at (1) (UNKNOWN/LOGICAL(4)) temp.f95:25:13: 25 | result = are_sentences_similar(sentence1, sentence2, xi, yi) | 1 Error: Function āare_sentences_similarā at (1) has no IMPLICIT type temp.f95:25:34: 25 | result = are_sentences_similar(sentence1, sentence2, xi, yi) | 1 Error: Return type mismatch of function āare_sentences_similarā at (1) (UNKNOWN/LOGICAL(4)) temp.f95:35:13: 35 | result = are_sentences_similar(sentence1, sentence2, xi, yi) | 1 Error: Function āare_sentences_similarā at (1) has no IMPLICIT type temp.f95:42:31: 42 | sentence2 = ["doubleplus ", "good "] | 1 Error: Different CHARACTER lengths (11/5) in array constructor at (1) temp.f95:25:34: 25 | result = are_sentences_similar(sentence1, sentence2, xi, yi) | 1 Error: Return type mismatch of function āare_sentences_similarā at (1) (UNKNOWN/LOGICAL(4)) temp.f95:47:13: 47 | result = are_sentences_similar(sentence1, sentence2, xi, yi) | 1 Error: Function āare_sentences_similarā at (1) has no IMPLICIT type temp.f95:25:57: 25 | result = are_sentences_similar(sentence1, sentence2, xi, yi) | 1 Error: Rank mismatch in argument āsimilarpairsā at (1) (rank-2 and rank-1) temp.f95:35:57: 35 | result = are_sentences_similar(sentence1, sentence2, xi, yi) | 1 Error: Rank mismatch in argument āsimilarpairsā at (1) (rank-2 and rank-1) temp.f95:47:57: 47 | result = are_sentences_similar(sentence1, sentence2, xi, yi) | 1 Error: Rank mismatch in argument āsimilarpairsā at (1) (rank-2 and rank-1)
module is_similar_sentence
contains
function is_similar_sentence(sentence1, sentence2, similar_pairs) result(is_similar)
implicit none
character(len=*), intent(in) :: sentence1(:), sentence2(:)
character(len=*), intent(in) :: similar_pairs(:, :)
logical :: is_similar
integer :: i, j, k
! Check if the sentences have the same length
if (size(sentence1) /= size(sentence2)) then
is_similar = .false.
return
end if
! Check if each word in sentence1 is similar to the corresponding word in sentence2
do i = 1, size(sentence1)
do j = 1, size(similar_pairs, 1)
if (sentence1(i) == similar_pairs(j, 1) .and. sentence2(i) == similar_pairs(j, 2)) then
exit
end if
end do
if (j > size(similar_pairs, 1)) then
is_similar = .false.
return
end if
end do
! If we reach this point, the sentences are similar
is_similar = .true.
end function is_similar_sentence
end module is_similar_sentence
program test_is_similar_sentence
use is_similar_sentence
implicit none
character(len=*), parameter :: sentence1(3) = ["I ", "am ", "happy "]
character(len=*), parameter :: sentence2(3) = ["I ", "am ", "happy "]
character(len=*), parameter :: similar_pairs(2, 2) = reshape([character(len=20) :: "I ", "am ", "happy ", "I ", "am ", "happy "], shape(similar_pairs))
logical :: is_similar
is_similar = is_similar_sentence(sentence1, sentence2, similar_pairs)
if (is_similar) then
print *, "The sentences are similar."
else
print *, "The sentences are not similar."
end if
! Test case 2:
character(len=*), parameter :: sentence3(2) = ["I ", "am "]
character(len=*), parameter :: sentence4(3) = ["I ", "am ", "happy "]
character(len=*), parameter :: similar_pairs2(1, 2) = reshape([character(len=20) :: "I ", "am "], shape(similar_pairs2))
is_similar = is_similar_sentence(sentence3, sentence4, similar_pairs2)
if (is_similar) then
print *, "The sentences are similar."
else
print *, "The sentences are not similar."
end if
! Test case 3:
character(len=*), parameter :: sentence5(2) = ["I ", "am "]
character(len=*), parameter :: sentence6(2) = ["I ", "am "]
character(len=*), parameter :: similar_pairs3(0, 2) = reshape([character(len=20) ::], shape(similar_pairs3))
is_similar = is_similar_sentence(sentence5, sentence6, similar_pairs3)
if (is_similar) then
print *, "The sentences are similar."
else
print *, "The sentences are not similar."
end if
end program test_is_similar_sentence
temp.f95:5:30: 5 | function is_similar_sentence(sentence1, sentence2, similar_pairs) result(is_similar) | 1 Error: MODULE attribute of āis_similar_sentenceā conflicts with PROCEDURE attribute at (1) temp.f95:6:17: 6 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:7:62: 7 | character(len=*), intent(in) :: sentence1(:), sentence2(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:55: 8 | character(len=*), intent(in) :: similar_pairs(:, :) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:25: 9 | logical :: is_similar | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:22: 10 | integer :: i, j, k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:48: 13 | if (size(sentence1) /= size(sentence2)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:14:26: 14 | is_similar = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:12: 15 | return | 1 Error: Unexpected RETURN statement in CONTAINS section at (1) temp.f95:16:7: 16 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:19:29: 19 | do i = 1, size(sentence1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:20:38: 20 | do j = 1, size(similar_pairs, 1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:21:95: 21 | if (sentence1(i) == similar_pairs(j, 1) .and. sentence2(i) == similar_pairs(j, 2)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:22:14: 22 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:23:11: 23 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:9: 24 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:42: 25 | if (j > size(similar_pairs, 1)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:26:28: 26 | is_similar = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:27:14: 27 | return | 1 Error: Unexpected RETURN statement in CONTAINS section at (1) temp.f95:28:9: 28 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:29:7: 29 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:32:23: 32 | is_similar = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:34:5: 34 | end function is_similar_sentence | 1 Error: Expecting END MODULE statement at (1) temp.f95:39:7: 39 | use is_similar_sentence | 1 Fatal Error: Cannot open module file āis_similar_sentence.modā for reading at (1): No such file or directory compilation terminated.
from collections import defaultdict
def areSentencesSimilar(sentence1, sentence2, similarPairs):
if len(sentence1) != len(sentence2): return False
similarityMap = defaultdict(set)
for pair in similarPairs:
similarityMap[pair[0]].add(pair[1])
similarityMap[pair[1]].add(pair[0])
for i in range(len(sentence1)):
if sentence1[i] != sentence2[i] and sentence2[i] not in similarityMap[sentence1[i]]:
return False
return True
1. If the lengths of the given sentences are unequal, the sentences cannot be similar, hence return false.
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
bool areSentencesSimilar(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
if (sentence1.size() != sentence2.size()) return false;
unordered_map<string, unordered_set<string>> similarityMap;
for (const auto& pair : similarPairs) {
similarityMap[pair[0]].insert(pair[1]);
similarityMap[pair[1]].insert(pair[0]);
}
for (int i = 0; i < sentence1.size(); ++i) {
if (sentence1[i] != sentence2[i] && !similarityMap[sentence1[i]].count(sentence2[i]))
return false;
}
return true;
}
1. If the lengths of the given sentences are unequal, the sentences cannot be similar, hence return false.