首页 > 编程语言 > 详细

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

时间:2016-05-10 02:15:01      阅读:125      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!