码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode] 205. Isomorphic Strings 解题思路 - Java

时间:2016-05-10 02:17:36      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

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; } }

 

[LeetCode] 205. Isomorphic Strings 解题思路 - Java

标签:

原文地址:http://www.cnblogs.com/TonyYPZhang/p/5476191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!