我找到了这个段落,我想在JS中实现它:
对于同构的两个字符串,字符串A中出现的所有字符都可以替换为另一个字符,以获得字符串B。必须保留字符的顺序。字符串A的每个字符到字符串B的每个字符必须有一对一的映射。
paper
和title
将返回true。egg
和sad
将返回false。dgg
和add
将返回true。
这是我的尝试:
console.log(isIsomorphic("egg", 'add')); // true
console.log(isIsomorphic("paper", 'title')); // true
console.log(isIsomorphic("kick", 'side')); // false
function isIsomorphic(firstString, secondString) {
// Check if the same lenght. If not, they cannot be isomorphic
if (firstString.length == secondString.length)
return false
var letterMap = {};
for (var i = 0; i < firstString.length; i++) {
var letterA = firstString[i],
letterB = secondString[i];
// If the letter does not exist, create a map and map it to the value
// of the second letter
if (letterMap[letterA] === undefined) {
letterMap[letterA] = letterB;
} else if (letterMap[letterA] !== letterB) {
// Eles if letterA already exists in the map, but it does not map to
// letterB, that means that A is mapping to more than one letter.
return false;
}
}
// If after iterating through and conditions are satisfied, return true.
// They are isomorphic
return true;
}
我搞不懂为什么其中有一个错误。
发布于 2018-09-11 02:51:08
你有一个简单的拼写错误:
if (firstString.length == secondString.length) {
return false
}
如果两个字符串的长度相同,则返回false。将其更改为:
if (firstString.length !== secondString.length) {
return false
}
console.log(isIsomorphic("egg", 'add')); // true
console.log(isIsomorphic("paper", 'title')); // true
console.log(isIsomorphic("kick", 'side')); // false
function isIsomorphic(firstString, secondString) {
// Check if the same lenght. If not, they cannot be isomorphic
if (firstString.length !== secondString.length) {
return false
}
var letterMap = {};
for (var i = 0; i < firstString.length; i++) {
var letterA = firstString[i],
letterB = secondString[i];
// If the letter does not exist, create a map and map it to the value
// of the second letter
if (letterMap[letterA] === undefined) {
letterMap[letterA] = letterB;
} else if (letterMap[letterA] !== letterB) {
// Eles if letterA already exists in the map, but it does not map to
// letterB, that means that A is mapping to more than one letter.
return false;
}
}
// If after iterating through and conditions are satisfied, return true.
// They are isomorphic
return true;
}
发布于 2018-09-11 03:29:34
您的解决方案几乎是正确的,ggorlen已经向您展示了修复方法。然而,我会给你另一个解决方案,这对我来说似乎更优雅。让我们引入同构签名的概念,它将是一个字符串,对于所有彼此同构的字符串都是相似的。
示例:
纸张: 0,2;1;3;4
标题: 0;2;1;3;4
鸡蛋: 0;1,2
悲伤: 0;1;2
dgg: 0;1,2
添加: 0;1,2
其思想是按照字母的出现顺序显示字母的索引。论文中的p在索引0和2上。a在索引1上。e在索引3上。r在索引4上。让我们实现获得同构签名的function
:
function getIsomorphicSignature(input) {
var helper = {};
for (var i = 0; i < input.length; i++) {
if (!helper[input[i]]) helper[input[i]] = [];
helper[input[i]].push(i);
}
var output = [];
for (var item in helper) output.push(helper[item].join(","));
return output.join(";");
}
现在,如果您想要查看两个字符串是否同构,那么可以比较它们的同构签名。这个解决方案的优雅之处在于你有一个可存储的属性,它在这种比较中说明了很多字符串。例如,如果您想要将同构字符串分组为集群,则可以这样做:
function clusterize(var inputStrings) {
var output = {};
for (int i = 0; i < inputStrings.length; i++) {
var signature = getIsomorphicSignature(inputStrings[i]);
if (!output[signature]) output[signature] = [];
output[signature].push(inputStrings[i]);
}
return output;
}
https://stackoverflow.com/questions/52263557
复制相似问题