LeetCode OJ 205 Isomorphic Strings
来源:互联网 发布:淘宝商家免费活动 编辑:程序博客网 时间:2024/05/20 10:22
Given two strings s andt, 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中的不同的字符,可以想象成一个加密过程,利用某种映射,将s加密成t
public class Solution { public boolean isIsomorphic(String s, String t) { //利用map保存映射关系 Map<Character,Character> map = new HashMap<Character,Character>(); for(int i = 0 ; i < s.length() ; i ++){ char key = s.charAt(i); char value = t.charAt(i); if(map.containsKey(key)){ if(value != map.get(key)){//存在一条key->x的映射,且x != value return false; } }else if(map.containsValue(value)){ return false;//已经存在一条x->value的映射,但是x != key }else{//不存在key->x映射和x->value的映射 map.put(key,value); } } return true; }}
搜了一下,感觉别人的算法跟我的差不多,没找到很高效的算法,为什么我的运行时间那么慢
0 0
- LeetCode OJ 205 Isomorphic Strings
- LeetCode OJ Isomorphic Strings
- <LeetCode OJ> 205. Isomorphic Strings
- LeetCode 205 - Isomorphic Strings
- [LeetCode 205]Isomorphic Strings
- LeetCode 205-Isomorphic Strings
- leetcode 205: Isomorphic Strings
- leetcode 205 Isomorphic Strings
- LeetCode 205:Isomorphic Strings
- leetcode-205 Isomorphic Strings
- Isomorphic Strings - LeetCode 205
- Isomorphic Strings leetcode 205
- leetcode 205:Isomorphic Strings
- leetcode-205 Isomorphic Strings
- Isomorphic Strings(leetcode 205)
- leetcode[205]:Isomorphic Strings
- leetcode-205-Isomorphic Strings
- leetcode 205: Isomorphic Strings
- 美国FICO评分系统简介
- 【Android开发】图形图像处理技术-绘制图片
- VS210 问题解决
- 计算机病毒特征
- P123.32(有57个人围成一个圈,顺序排号,从第一个人开始报数(从1~4报数),凡报到四的人退出圈子,问最后留下的是原来的几号?)
- LeetCode OJ 205 Isomorphic Strings
- Rails中使用flash总结
- c++之超越c的语法02-类型转换
- Linux 远程登录 Windows
- 获取手机联系人
- 【Android开发】范例1-绘制Android的机器人
- Linux网络实验1-wget+lynx
- DreamWeaver关于Wamp站点设置方法
- 剑指offer 面试题28—字符串的排列