确定同构字符串JavaScript
如果可以替换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
热门推荐
10 送外卖祝福语大全简短
11 父亲祝福语大全简短语
12 订婚祝福语简短好听女
13 国庆求婚祝福语简短精辟
14 祝福语怎么写大全简短
15 许嵩生日祝福语简短
16 牛年女神节祝福语简短
17 圣诞祝福语简短文字
18 老师出国祝福语大全简短