みさご解体新書

回文判定

コード例

C++

using namespace std;

bool is_palindrome(string s) {
  string t = s;
  reverse(t.begin(), t.end());

  return s == t;
}

TypeScript

function isPalindrome(s: string): boolean {
  const t = s.split("").reverse().join("");
  return s == t;
}

アルゴリズム

文字列 S を回文判定する場合、S を反転した文字列 T を用意して、S と T が同じ文字列かどうかを確認する。