lintcode(638)Strings Homomorphism
来源:互联网 发布:水利工程量绘图软件 编辑:程序博客网 时间:2024/05/08 16:18
描述:
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.
注意事项
You may assume both s and t have the same length
.
样例:
Given s = "egg"
, t = "add"
, return true
.
Given s = "foo"
, t = "bar"
, return false
.
Given s = "paper"
, t = "title"
, return true
.
思路:
判断对应位置字符是否成唯一映射,同时判断是否出现重复映射。
public class Solution { /** * @param s a string * @param t a string * @return true if the characters in s can be replaced to get t or false */ public boolean isIsomorphic(String s, String t) { // Write your code here HashMap<Character , Character> record = new HashMap<>(); HashSet<Character> repeat = new HashSet<>(); for(int i = 0;i<s.length();i++){ if(!record.containsKey(s.charAt(i))){ if(repeat.contains(t.charAt(i))){ return false; } record.put(s.charAt(i) , t.charAt(i)); repeat.add(t.charAt(i)); }else{ if(record.get(s.charAt(i)) != t.charAt(i)){ return false; } } } return true; }}
0 0
- lintcode(638)Strings Homomorphism
- LintCode之Strings Homomorphism
- LintCode python 小白-简单题-638-Strings Homomorphism
- Strings Homomorphism
- Strings Homomorphism
- Strings Homomorphism
- Strings Homomorphism
- Two Strings Are Anagrams-LintCode
- 【LintCode】 Compare Strings 比较字符串
- [LintCode]Compare Strings(C++|Python)
- lintcode 比较字符串(Compare Strings ) (Java)
- Lie group homomorphism
- LintCode Two Strings Are Anagrams 两个字符串是变位词
- [LintCode]Two Strings Are Anagrams(C++|Java|Python)
- Strings
- Strings
- strings
- Strings
- linux_清空文件内容的六种方法
- 【Python开发】PyInstaller打包Python程序
- MySQL InnoDB的一致性非锁定读
- 京东实用待产清单!有了大宝的经验,杜绝浪费~
- javascript array.forEach()和array.map()的区别
- lintcode(638)Strings Homomorphism
- 希尔排序_Java
- Android 加载assets文件夹下带有css的html文件,无法显示网络请求的结果,原因是onReceiveSslError问题
- Java中hashCode的作用
- 解决报错:java.lang.NoSuchMethodException: com.tangyuan.entity.RicherProduct.<init>()
- Java异常finally语句
- 如何手动对apk进行签名?
- JCO连接SAP例子
- 队列ADT