Generation

generate functionSun, 26 Mar 2023

# Given a pair of strings. The goal is to make them equal by doing one of the following operations at most once: Select two characters, char? and char2, and replace all occurrences of char1 in the first string with char2. Select two characters, char? and char2, and replace all occurrences of char in the second string with char2. For example, the string "xxyz" can be converted to any of the following strings by performing the operation as mentioned above: "yyyz", "wwy2", "voz", "ewz", etc. However, it cannot be converted to the string "yzyz" (Since all the occurrences of a character can be replaced by a single new character). Note that doing both operations simultaneously is not allowed. For example, given two strings "xz" and "zx", we can not convert both the x's in the respective strings to 'z' to make them equal. Given two arrays of nsstrings each, compare the strings present at the same indices in both the arrays and find if they can be made equal or not. Return an array of n strings, where the /*” element would be YES if the strings at index /can be equalized; otherwise, the answer is NO.

``````function makeEqual(a, b) {
const lena = a.length;
const lenb = b.length;
const results = [];
for (let i = 0; i < Math.min(lena, lenb); i++) {
if (a[i] === b[i]) {
results.push('YES');
} else if (a[i].length === 1 && b[i].length === 1) {
results.push('NO');
} else if (a[i].length === 1) {
if (b[i].includes(a[i])) {
results.push('YES');
} else {
results.push('NO');
}
} else if (b[i].length === 1) {
if (a[i].includes(b[i])) {
results.push('YES');
} else {
results.push('NO');
}
} else {
results.push('NO');
}
}
return results;
}``````
Javascript
Generate More