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

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

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

问题

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

  1. function isIsogram(str){
  2. let NewStr = str.toLowerCase();
  3. for ( let i = 0; i < NewStr.length; i++){
  4. for ( let j = i + 1; j < NewStr.length; j++) {
  5. if ( NewStr[i] === NewStr[j]) {
  6. return false }
  7. else {
  8. return true}
  9. }
  10. }
  11. }
英文:

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.

  1. function isIsogram(str){
  2. let NewStr = str.toLowerCase();
  3. for ( let i = 0; i &lt; NewStr.length; i++){
  4. for ( let j = i + 1; j &lt; NewStr.length; j++) {
  5. if ( NewStr[i] === NewStr[j]) {
  6. return false }
  7. else {
  8. return true}
  9. }
  10. }
  11. }

答案1

得分: 2

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

  1. if (NewStr[i] === NewStr[j]) {
  2. return false;
  3. } else {
  4. return true;
  5. }

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

  1. function isIsogram(str) {
  2. let NewStr = str.toLowerCase();
  3. for (let i = 0; i < NewStr.length; i++) {
  4. for (let j = i + 1; j < NewStr.length; j++) {
  5. if (NewStr[i] === NewStr[j]) {
  6. return false;
  7. }
  8. }
  9. }
  10. return true;
  11. }

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

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

希望这有帮助。

英文:

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

  1. if ( NewStr[i] === NewStr[j]) {
  2. return false }
  3. else {
  4. 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 -->

  1. function isIsogram(str) {
  2. let NewStr = str.toLowerCase();
  3. for (let i = 0; i &lt; NewStr.length; i++) {
  4. for (let j = i + 1; j &lt; NewStr.length; j++) {
  5. if (NewStr[i] === NewStr[j]) {
  6. return false
  7. }
  8. }
  9. }
  10. return true;
  11. }
  12. console.log(isIsogram(&#39;cdba&#39;)); // true
  13. console.log(isIsogram(&#39;aa&#39;)); // false
  14. 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 -->

  1. function isIsogram(str) {
  2. return new Set(str.toLowerCase()).size === str.length;
  3. }
  4. console.log(isIsogram(&#39;cdba&#39;)); // true
  5. console.log(isIsogram(&#39;aa&#39;)); // false
  6. console.log(isIsogram(&#39;efgaba&#39;)); // false

<!-- end snippet -->

答案2

得分: 2

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

英文:

look what did your code do:

  1. if ( NewStr[i] === NewStr[j]) {
  2. return false }
  3. else {
  4. 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. 只使用一个循环。

结论:

  1. function isIso(string) {
  2. var seen = {},
  3. i;
  4. string = string.toLowerCase();
  5. for (i = 0; i < string.length; i++) {
  6. if (seen[string[i]]) return false;
  7. seen[string[i]] = true;
  8. }
  9. return true;
  10. }
  11. console.log(isIso('')); // true
  12. console.log(isIso('abc')); // true
  13. console.log(isIso('abbc')); // false
  14. 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;

  1. string = string.toLowerCase();
  2. for (i = 0; i &lt; string.length; i++) {
  3. if (seen[string[i]]) return false;
  4. seen[string[i]] = true;
  5. }
  6. return true;
  7. }
  8. console.log(isIso(&#39;&#39;)); // true
  9. console.log(isIso(&#39;abc&#39;)); // true
  10. console.log(isIso(&#39;abbc&#39;)); // false
  11. console.log(isIso(&#39;abca&#39;)); // false

<!-- end snippet -->

答案4

得分: 1

现代 JavaScript 代码如下:

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

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

英文:

Modern javascript code is simple as

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

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

  1. function isIsogram(str){
  2. return str.length === new Set(str.toLowerCase().split(&#39;&#39;)).size;
  3. }
  4. console.log(isIsogram(&#39;abcd&#39;))
  5. 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
}

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

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:

确定