Easy 205题 Isomorphic Strings
来源:互联网 发布:java整型转16进制 编辑:程序博客网 时间:2024/05/16 08:17
Question:
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.
Solution:
public class Solution { public boolean isIsomorphic(String s, String t) { Map<Character,Character> map=new HashMap<Character, Character>(); if(s.length()!=t.length()) return false; int len=s.length(); for(int i=0;i<=len-1;i++) { char sa=s.charAt(i); char ta=t.charAt(i); if(map.containsKey(sa)) { if(!(map.get(sa)==ta)) return false; } else { if(map.containsValue(ta)) return false; map.put(sa,ta); } } return true; }}discuss里有一个很简单的方法,但是需要搞懂,put的返回值是什么,如果put了一个原来没有的key,则返回值为null;如果put了一个原来有的key,则返回旧的value,这里就是老key的位置
public boolean isIsomorphic(String s, String t) { Map m = new HashMap(); for (Integer i=0; i<s.length(); ++i) if (m.put(s.charAt(i), i) != m.put(t.charAt(i)+"", i)) return false; return true;}
0 0
- Easy 205题 Isomorphic Strings
- Letcode 205[easy]--- Isomorphic Strings. Isomorphic Strings
- [Leetcode 205, Easy] Isomorphic Strings
- <LeetCode><Easy> 205 Isomorphic Strings --HashTable
- 205. Isomorphic Strings [easy] (Python)
- Easy-题目46:205. Isomorphic Strings
- Leetcode 205. Isomorphic Strings (Easy) (cpp)
- Leetcode 第205题 Isomorphic Strings
- LeetCode 205 - Isomorphic Strings
- [LeetCode 205]Isomorphic Strings
- LeetCode 205-Isomorphic Strings
- leetcode 205: Isomorphic Strings
- leetcode 205 Isomorphic Strings
- 205Isomorphic Strings
- LeetCode 205:Isomorphic Strings
- 205 Isomorphic Strings
- leetcode-205 Isomorphic Strings
- Isomorphic Strings - LeetCode 205
- Medium 213题 House Robber II
- 第六章 个人优化vim第三步 :主题colorscheme
- 设计模式 - Singleton in Java
- Leetcode 369. Plus One Linked List (Medium) (cpp)
- touchpanel驱动
- Easy 205题 Isomorphic Strings
- 198. House Robber
- Java中Map按value值进行排序
- TortoiseSVN提交commit提示Unable to create pristine install stream.系统找不到指定的路径
- 200. Number of Islands
- Leetcode 4. Median of Two Sorted Arrays (Hard) (cpp)
- swift项目:微博
- 第七章 个人优化vim第四步:插件集合spf13安装与使用教程
- Ueditor使用方法及其若干问题的解决