スポンサーリンク

【LeetCode】205. Isomorphic Strings 解答・解説【Python】

スポンサーリンク
スポンサーリンク
この記事は約2分で読めます。

 

問題

原文

Given two strings s and tdetermine if they are isomorphic.

Two strings s and t are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

 

Example 1:

Example 2:

Example 3:

 

Constraints:

  • 1 <= s.length <= 5 * 104
  • t.length == s.length
  • s and t consist of any valid ascii character.

 

内容

2つの文字列sとtが与えられたとき、それらが同型であるかどうかを判定しなさい。

2つの文字列sとtは、sの文字を置き換えてtを得ることができる場合、同型である。

ある文字の出現箇所はすべて、文字の順序を保ったまま別の文字に置き換える必要がある。2つの文字が同じ文字に対応することはできないが、1つの文字がそれ自身に対応することはできる。

 

※正しくない可能性があります。

 

解答

解答1:Python

sとtの各文字のインデックスを新たなリストに記録しておき、記録したリストどうしが同値であるかを判定する。

 

解答2:

 

 

 

メモ・参考・感想

■感想

最初はハッシュテーブルを使った解答を作っていたのだけど、添字を使った要素の指定が下手なので要練習。

 

 

前:724. Find Pivot Index

次:191. Number of 1 Bits

LeetCode 解答・解説記事一覧

コメント