1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

NeetCode_ValidPalindrome

Posted at

はじめに

転職の内定が決まった。
しかし、ある会社でコーディングテストがあったが
全く歯が立たんかった。
克服したいので、NeetCodeを少しずつ進めていく。
ひとまずEasyのものから。
その際の回答に必要だった知識や気づきを書いていこうと思う。

挑戦した問題

問題の概要

Given a string s, return true if it is a palindrome, otherwise return false.

A palindrome is a string that reads the same forward and backward. It is also case-insensitive and ignores all non-alphanumeric characters.

Note: Alphanumeric characters consist of letters (A-Z, a-z) and numbers (0-9).

Example

Input: s = "Was it a car or a cat I saw?"

Output: true

回答の流れ

  • 空文字列を作成する
  • 文字や数字なら文字列に追加していく
  • 文字列と反転文字列が同一かチェックする

回答のために必要だった知識

// 対象の文字が文字か数字かをチェックする
if (char.IsLetterOrDigit(c))
// 文字列を配列としてとらえ、反転させ、配列に変換している
    // ReverseによりIEnumerable<char>になる
    // new string(char[])を作成するためにToArray()

return newStr ==new string(newStr.Reverse().ToArray());

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?