Isomorphic Strings
题目地址:
https://leetcode.com/problems/isomorphic-strings/description/
题目:
解题思路:
这道题就是用map去重。
代码:
https://leetcode.com/problems/isomorphic-strings/description/
题目:
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
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.
You may assume both s and t have the same length.
解题思路:
这道题就是用map去重。
代码:
public boolean isIsomorphic(String s, String t) { Map<Character, Character> map = new HashMap<>(); int len = s.length(); for(int i = 0; i <= len - 1; i++){ char cs = s.charAt(i); char ct = t.charAt(i); if(map.containsKey(cs)){ if(map.get(cs) != ct){ return false; } } else{ if(map.values().contains(ct)){ return false; } else{ map.put(cs, ct); } } } return true; }

Comments
Post a Comment