【leetcode】【205】Isomorphic Strings

来源:互联网 发布:淘宝卖家新手教程 编辑:程序博客网 时间:2024/06/05 11:23

一、问题描述

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.

二、问题分析

显然是一种map的关系,可以使用HashMap来处理,比较简单。

三、Java AC代码

public boolean isIsomorphic(String s, String t) {        HashMap<Character, Character> map = new HashMap<Character, Character>();if (s.length()!=t.length()) {return false;}char chs;char cht;for(int i=0;i<s.length();i++){chs = s.charAt(i);cht = t.charAt(i);if (!map.containsKey(chs)) {if (map.values().contains(cht)) {return false;}map.put(chs, cht);}else {if (map.get(chs).equals(cht)) {continue;}else {return false;}}}return true;    }


0 0