[LeetCode] 205. Isomorphic Strings 解题思路

Given two strings s and t, determine if they are isomorphic.

Two strings 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.

For example,
Given "egg""add", return true.

Given "foo""bar", return false.

Given "paper""title", return true.

Note:
You may assume both s and t have the same length.

问题:给定两个字符串,判断他们是否同构。

当字符串 s 中的字符可以被替换得到另一个字符串 t , 则表示字符串 s 和 t 是同构。

import static java.lang.System.out;

import java.util.Hashtable;

public class Solution {
    public boolean isIsomorphic(String s, String t) {
Hashtable
<Character, Character> s_t = new Hashtable<Character, Character>(); Hashtable<Character, Character> t_s = new Hashtable<Character, Character>(); int length = s.length(); for(int i = 0; i < length; i++){
Character ss
= s.charAt(i); Character tt = t.charAt(i); if(s_t.containsKey(ss) || t_s.containsKey(tt)){ if(s_t.get(ss) == tt && t_s.get(tt) == ss){ continue; }else{ return false; } }else{ s_t.put(ss, tt); t_s.put(tt, ss); } } return true; } }
原文地址:https://www.cnblogs.com/TonyYPZhang/p/5476191.html