2020-09-29 22:09:05 +02:00

3.2 KiB
Raw Blame History

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
aaa48de84e1ecc7c742e1124 Palindrome Checker 5 16004 回文检查器

Description

如果给定的一个字符串是回文,那么返回true,否则返回falsepalindrome回文,指在忽略标点符号、大小写和空格的前提下,正着读和反着读一模一样。 注意:
检查回文时,你需要先除去所有非字母数字的字符(标点、空格和符号)并且将所有字符转换成字母大写或字母小写。 我们将会传入不同格式的字符串,例如:"racecar""RaceCar""race CAR"等等。 我们也会传入一些包含特殊符号的字符串,例如"2A3*3a2""2A3 3a2""2_A3*3#A2"

Instructions

Tests

tests:
  - text: "<code>palindrome('eye')</code>应该返回一个布尔值。"
    testString: assert(typeof palindrome("eye") === "boolean");
  - text: "<code>palindrome('eye')</code>应该返回 true。"
    testString: assert(palindrome("eye") === true);
  - text: "<code>palindrome('_eye')</code>应该返回 true。"
    testString: assert(palindrome("_eye") === true);
  - text: "<code>palindrome('race car')</code>应该返回 true。"
    testString: assert(palindrome("race car") === true);
  - text: "<code>palindrome('not a palindrome')</code>应该返回 false。"
    testString: assert(palindrome("not a palindrome") === false);
  - text: "<code>palindrome('A man, a plan, a canal. Panama')</code>应该返回 true。"
    testString: assert(palindrome("A man, a plan, a canal. Panama") === true);
  - text: "<code>palindrome('never odd or even')</code>应该返回 true。"
    testString: assert(palindrome("never odd or even") === true);
  - text: "<code>palindrome('nope')</code>应该返回 false。"
    testString: assert(palindrome("nope") === false);
  - text: "<code>palindrome('almostomla')</code>应该返回 false。"
    testString: assert(palindrome("almostomla") === false);
  - text: "<code>palindrome('My age is 0, 0 si ega ym.')</code>应该返回 true。"
    testString: assert(palindrome("My age is 0, 0 si ega ym.") === true);
  - text: "<code>palindrome('1 eye for of 1 eye.')</code>应该返回 false。"
    testString: assert(palindrome("1 eye for of 1 eye.") === false);
  - text: '<code>palindrome("0_0 (: /-\ :) 0-0")</code>应该返回 true。'
    testString: 'assert(palindrome("0_0 (: /-\ :) 0-0") === true);'
  - text: "<code>palindrome('five|\_/|four')</code>应该返回 false。"
    testString: assert(palindrome("five|\_/|four") === false);

Challenge Seed

function palindrome(str) {
  // Good luck!
  return true;
}



palindrome("eye");

Solution

function palindrome(str) {
  var string = str.toLowerCase().split(/[^A-Za-z0-9]/gi).join('');
  var aux = string.split('');
  if (aux.join('') === aux.reverse().join('')){
    return true;
  }

  return false;
}