#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
const char **first;
const char **second;
int capacity;
} PairSet;
static unsigned int hash_pair(const char *a, const char *b) {
unsigned int h = 5381;
while (*a) h = h * 33 + (unsigned char)*a++;
h = h * 33 + '#';
while (*b) h = h * 33 + (unsigned char)*b++;
return h;
}
static PairSet pairset_create(int capacity) {
PairSet set;
set.capacity = capacity;
set.first = calloc(capacity, sizeof(char *));
set.second = calloc(capacity, sizeof(char *));
return set;
}
static void pairset_free(PairSet *set) {
free(set->first);
free(set->second);
}
static void pairset_insert(PairSet *set, const char *a, const char *b) {
unsigned int idx = hash_pair(a, b) % set->capacity;
while (set->first[idx]) {
if (!strcmp(set->first[idx], a) && !strcmp(set->second[idx], b)) {
return;
}
idx = (idx + 1) % set->capacity;
}
set->first[idx] = a;
set->second[idx] = b;
}
static bool pairset_contains(const PairSet *set, const char *a, const char *b) {
unsigned int idx = hash_pair(a, b) % set->capacity;
while (set->first[idx]) {
if (!strcmp(set->first[idx], a) && !strcmp(set->second[idx], b)) {
return true;
}
idx = (idx + 1) % set->capacity;
}
return false;
}
bool areSentencesSimilar(char **sentence1, int sentence1Size, char **sentence2, int sentence2Size,
char ***similarPairs, int similarPairsSize, int *similarPairsColSize) {
(void)similarPairsColSize;
if (sentence1Size != sentence2Size) {
return false;
}
PairSet pairs = pairset_create(similarPairsSize * 2 + 1);
for (int i = 0; i < similarPairsSize; i++) {
pairset_insert(&pairs, similarPairs[i][0], similarPairs[i][1]);
}
bool similar = true;
for (int i = 0; i < sentence1Size; i++) {
const char *w1 = sentence1[i];
const char *w2 = sentence2[i];
if (strcmp(w1, w2) && !pairset_contains(&pairs, w1, w2) && !pairset_contains(&pairs, w2, w1)) {
similar = false;
break;
}
}
pairset_free(&pairs);
return similar;
}
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
bool areSentencesSimilar(vector<string> &sentence1, vector<string> &sentence2,
vector<vector<string>> &similarPairs) {
if (sentence1.size() != sentence2.size())
return false;
unordered_set<string> pairs;
for (const auto &p : similarPairs)
pairs.insert(p[0] + "#" + p[1]);
for (size_t i = 0; i < sentence1.size(); i++) {
const string &w1 = sentence1[i];
const string &w2 = sentence2[i];
if (w1 != w2 && !pairs.count(w1 + "#" + w2) && !pairs.count(w2 + "#" + w1))
return false;
}
return true;
}
};
package solution
func areSentencesSimilar(sentence1 []string, sentence2 []string, similarPairs [][]string) bool {
if len(sentence1) != len(sentence2) {
return false
}
type pair struct{ a, b string }
pairs := make(map[pair]struct{}, len(similarPairs))
for _, p := range similarPairs {
pairs[pair{p[0], p[1]}] = struct{}{}
}
for i, w1 := range sentence1 {
w2 := sentence2[i]
if w1 == w2 {
continue
}
_, fwd := pairs[pair{w1, w2}]
_, bwd := pairs[pair{w2, w1}]
if !fwd && !bwd {
return false
}
}
return true
}
import java.util.HashSet;
import java.util.List;
import java.util.Set;
class Solution {
public boolean areSentencesSimilar(String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {
if (sentence1.length != sentence2.length) {
return false;
}
Set<String> pairs = new HashSet<>();
for (List<String> p : similarPairs) {
pairs.add(p.get(0) + "#" + p.get(1));
}
for (int i = 0; i < sentence1.length; i++) {
String w1 = sentence1[i];
String w2 = sentence2[i];
if (!w1.equals(w2) && !pairs.contains(w1 + "#" + w2) && !pairs.contains(w2 + "#" + w1)) {
return false;
}
}
return true;
}
}
/**
* @param {string[]} sentence1
* @param {string[]} sentence2
* @param {string[][]} similarPairs
* @return {boolean}
*/
var areSentencesSimilar = function (sentence1, sentence2, similarPairs) {
if (sentence1.length !== sentence2.length) {
return false;
}
const key = (a, b) => `${a}#${b}`;
const pairs = new Set(similarPairs.map(([a, b]) => key(a, b)));
for (let i = 0; i < sentence1.length; i++) {
const w1 = sentence1[i];
const w2 = sentence2[i];
if (w1 !== w2 && !pairs.has(key(w1, w2)) && !pairs.has(key(w2, w1))) {
return false;
}
}
return true;
};
class Solution:
def areSentencesSimilar(
self, sentence1: list[str], sentence2: list[str], similarPairs: list[list[str]]
) -> bool:
if len(sentence1) != len(sentence2):
return False
pairs = {(a, b) for a, b in similarPairs}
for w1, w2 in zip(sentence1, sentence2):
if w1 != w2 and (w1, w2) not in pairs and (w2, w1) not in pairs:
return False
return True
use std::collections::HashSet;
impl Solution {
pub fn are_sentences_similar(
sentence1: Vec<String>,
sentence2: Vec<String>,
similar_pairs: Vec<Vec<String>>,
) -> bool {
if sentence1.len() != sentence2.len() {
return false;
}
let pairs: HashSet<(&str, &str)> = similar_pairs
.iter()
.map(|p| (p[0].as_str(), p[1].as_str()))
.collect();
sentence1.iter().zip(&sentence2).all(|(w1, w2)| {
w1 == w2
|| pairs.contains(&(w1.as_str(), w2.as_str()))
|| pairs.contains(&(w2.as_str(), w1.as_str()))
})
}
}
struct Solution;