为什么如果相同的字符不相邻,输出结果就不正确?

huangapple go评论77阅读模式
英文:

Why the output is not correct if the same characters are not adjacent?

问题

一个"异位同字母词"是指一个单词中没有重复的字母,无论是连续的还是非连续的。实现一个函数来确定一个只包含字母的字符串是否是一个"异位同字母词"。假设空字符串也是一个"异位同字母词"。忽略字母的大小写。

function isIsogram(str){
  let NewStr = str.toLowerCase();
     for ( let i = 0; i < NewStr.length; i++){
        for ( let j = i + 1; j < NewStr.length; j++) {
            if ( NewStr[i] === NewStr[j]) {
                return false } 
            else {
                return true}
      }
    }
}
英文:

An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.

function isIsogram(str){
  let NewStr = str.toLowerCase();
     for ( let i = 0; i &lt; NewStr.length; i++){
        for ( let j = i + 1; j &lt; NewStr.length; j++) {
            if ( NewStr[i] === NewStr[j]) {
                return false } 
            else {
                return true}
      }
    }
}

答案1

得分: 2

这段代码会在找到两个不相等的字母时返回true

if (NewStr[i] === NewStr[j]) {
    return false;
} else {
    return true;
}

你应该在条件满足时返回false。如果条件不满足,继续循环,最后返回true

function isIsogram(str) {
  let NewStr = str.toLowerCase();
  for (let i = 0; i < NewStr.length; i++) {
    for (let j = i + 1; j < NewStr.length; j++) {
      if (NewStr[i] === NewStr[j]) {
        return false;
      }
    }
  }
  
  return true;
}

你也可以使用Set来执行相同的检查。如果Set中的字母数等于原始字符串的字母数,那么所有字符都是唯一的:

function isIsogram(str) {
  return new Set(str.toLowerCase()).size === str.length;
}

希望这有帮助。

英文:

This code will return true as soon as it finds two letters that are not equal:

if ( NewStr[i] === NewStr[j]) {
    return false } 
else {
    return true}

What you should do is return false only if the condition passes. If not continue the loop, and return true at the end:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function isIsogram(str) {
  let NewStr = str.toLowerCase();
  for (let i = 0; i &lt; NewStr.length; i++) {
    for (let j = i + 1; j &lt; NewStr.length; j++) {
      if (NewStr[i] === NewStr[j]) {
        return false
      }
    }
  }
  
  return true;
}

console.log(isIsogram(&#39;cdba&#39;)); // true
console.log(isIsogram(&#39;aa&#39;)); // false
console.log(isIsogram(&#39;efgaba&#39;)); // false

<!-- end snippet -->

You can also do the same check using a Set. If the number of letters in the Set is equal to the number of letters in the original string, all characters are unique.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function isIsogram(str) {
  return new Set(str.toLowerCase()).size === str.length;
}

console.log(isIsogram(&#39;cdba&#39;)); // true
console.log(isIsogram(&#39;aa&#39;)); // false
console.log(isIsogram(&#39;efgaba&#39;)); // false

<!-- end snippet -->

答案2

得分: 2

当条件为真时,返回 false,否则返回 true。这意味着函数将在第一次结束,无论 NewStr[0] === NewStr[0] 是否为真。

英文:

look what did your code do:

 if ( NewStr[i] === NewStr[j]) {
                return false } 
            else {
                return true}}

When the condition is true, return false, otherwise return true.
It means that function will be finished at the first time whether NewStr[0] === NewStr[0] is true or not

答案3

得分: 1

  1. 存储已经看到的字母。如果之前见过该字母,则返回 false

  2. 使用对象来存储已经看到的字母,因为如果字母是未见过的,属性会返回 false

  3. 只使用一个循环。

结论:

function isIso(string) {
    var seen = {},
        i;
    
    string = string.toLowerCase();
    for (i = 0; i < string.length; i++) {
        if (seen[string[i]]) return false;
        seen[string[i]] = true;    
    }
    return true;
}

console.log(isIso(''));     // true
console.log(isIso('abc'));  // true
console.log(isIso('abbc')); // false
console.log(isIso('abca')); // false
英文:

What you can do:

  1. Store a seen letter. If you have seen a letter before return false.

  2. Use an object for for storing a seel letter, because if unseen, the property returns false

  3. Use only a single loop.

Conclusion:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function isIso(string) {
var seen = {},
i;

    string = string.toLowerCase();
    for (i = 0; i &lt; string.length; i++) {
        if (seen[string[i]]) return false;
        seen[string[i]] = true;    
    }
    return true;
}

console.log(isIso(&#39;&#39;));     // true
console.log(isIso(&#39;abc&#39;));  // true
console.log(isIso(&#39;abbc&#39;)); // false
console.log(isIso(&#39;abca&#39;)); // false

<!-- end snippet -->

答案4

得分: 1

现代 JavaScript 代码如下:

function isIsogram(str){
  return str.length === new Set(str.toLowerCase().split('')).size;
}
console.log(isIsogram('abcd'))
console.log(isIsogram('abcb'))

请注意,上述代码是 JavaScript 代码示例,用于检查一个字符串是否为异位词(isIsogram)。

英文:

Modern javascript code is simple as

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function isIsogram(str){
  return str.length === new Set(str.toLowerCase().split(&#39;&#39;)).size;
}
console.log(isIsogram(&#39;abcd&#39;))
console.log(isIsogram(&#39;abcb&#39;))

<!-- end snippet -->

答案5

得分: 0

function isIsogram(str){
for(let i=0; i<str.length; i++){
if(str[i] === str[i+1]){
return false
}
}
return true
}

英文:
function isIsogram(str){
    for(let i=0; i&lt;str.length; i++){
        if(str[i] === str[i+1]){ 
            return false
        }   
     }  
   return true 
  } 

huangapple
  • 本文由 发表于 2020年1月3日 15:52:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定