判断同构字符串 JavaScript
javascriptweb developmentfront end technologyobject oriented programming更新于 2024/1/3 8:44:17
如果 str1 中的字符可以替换为 str2,则两个字符串(str1 和 str2)是同构的。
例如 −
const str1 = 'abcde'; const str2 = 'eabdc';
这两个是同构字符串的示例
我们需要编写一个 JavaScript 函数,该函数用于判断两个输入字符串是否同构。
示例
const str1 = 'abcde';
const str2 = 'eabdc';
const isIsomorphic = (str1 = '', str2 = '') => {
if (str1.length !== str2.length) {
return false;
};
for (let i = 0;
i < str1.length; i++) {
const a = str1.indexOf(str1[i]);
const b = str2.indexOf(str2[i]);
if (str2[a] !== str2[i] || str1[b] !== str1[i]) {
return false;
};
};
return true;
};
console.log(isIsomorphic(str1, str2));
输出
控制台中的输出将是 −
true
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

