首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >检查给定的字符串是否同构

检查给定的字符串是否同构
EN

Stack Overflow用户
提问于 2018-09-11 02:31:13
回答 2查看 1.1K关注 0票数 4

我找到了这个段落,我想在JS中实现它:

对于同构的两个字符串,字符串A中出现的所有字符都可以替换为另一个字符,以获得字符串B。必须保留字符的顺序。字符串A的每个字符到字符串B的每个字符必须有一对一的映射。

papertitle将返回true。eggsad将返回false。dggadd将返回true。

这是我的尝试:

代码语言:javascript
复制
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;
}

我搞不懂为什么其中有一个错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-11 02:51:08

你有一个简单的拼写错误:

代码语言:javascript
复制
  if (firstString.length == secondString.length) {
    return false
  }

如果两个字符串的长度相同,则返回false。将其更改为:

代码语言:javascript
复制
  if (firstString.length !== secondString.length) {
    return false
  }

代码语言:javascript
复制
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;
}

票数 3
EN

Stack Overflow用户

发布于 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

代码语言:javascript
复制
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(";");
}

现在,如果您想要查看两个字符串是否同构,那么可以比较它们的同构签名。这个解决方案的优雅之处在于你有一个可存储的属性,它在这种比较中说明了很多字符串。例如,如果您想要将同构字符串分组为集群,则可以这样做:

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52263557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档