Java for 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.

解题思路:

用两张图保存下两个方向的映射即可,JAVA实现如下:

    public boolean isIsomorphic(String s, String t) {
		HashMap<Character, Character> hm = new HashMap<Character, Character>();
		HashMap<Character, Character> hm2 = new HashMap<Character, Character>();
		for (int i = 0; i < s.length(); i++) {
			if (hm.containsKey(s.charAt(i))
					&& hm.get(s.charAt(i)) != t.charAt(i))
				return false;
			hm.put(s.charAt(i), t.charAt(i));
			if (hm2.containsKey(t.charAt(i))
					&& hm2.get(t.charAt(i)) != s.charAt(i))
				return false;
			hm2.put(t.charAt(i), s.charAt(i));
		}
		return true;
    }
时间: 2024-11-02 23:37:00

Java for LeetCode 205 Isomorphic Strings的相关文章

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

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.

Java [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 charac

LeetCode 205 Isomorphic Strings

Problem: 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 cha

LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

翻译 给定两个字符串s和t,决定它们是否是同构的. 如果s中的元素被替换可以得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换另一个字符串的元素的过程中,所有字符的顺序必须保留. 没有两个字符可以被映射到相同的字符,但字符可以映射到该字符本身. 例如, 给定"egg","add",返回真. 给定"foo","bar",返回假. 给定"paper","title",返回真. 批

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.

[leetcode]205. Isomorphic Strings同构字符串

哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查另外一个map是不是也是对应位置重复 */ if (s.length()!=t.length()) { return false; } Map<Character,Integer> map1 = new HashMap<>(); Map<Character,Integer>

LeetCode:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: 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 occurre

205. Isomorphic Strings - LeetCode

Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中相同位置字符的差是否相同 Java实现: public boolean isIsomorphic(String s, String t) { Map<String, Integer> map = new HashMap<>(); for (int i=0; i<s.length(

【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.