Strings Homomorphism
来源:互联网 发布:淘宝买汽车配件 编辑:程序博客网 时间:2024/05/30 02:25
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
.
样例
题意: 给两个字符串s和t,看他们是否是同构的,如果s中的字符能被t中的字符替换,所有字符的出现都必须用另一个字符替换,同时保留字符的顺序,没有两个字符可以映射到同一个字符,但字符可以映射到自己。Given s = "egg"
, t = "add"
, return true
.
Given s = "foo"
, t = "bar"
, return false
.
Given s = "paper"
, t = "title"
, return true
.
思路: 利用map
private Map<Character, Character> map = new HashMap<>(); /** * @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) { if (s.length() != t.length()) return false; map.clear(); boolean isIsomorphic = true; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); Object o = map.get(c); if (o == null) { if (findKeyInMap(t.charAt(i))) { isIsomorphic = false; break; } else { map.put(c, t.charAt(i)); } } else if (((Character) o).charValue() != t.charAt(i)) { isIsomorphic = false; break; } } return isIsomorphic; } private boolean findKeyInMap(char c) { return map.containsValue(c); }
阅读全文
0 0
- Strings Homomorphism
- Strings Homomorphism
- Strings Homomorphism
- Strings Homomorphism
- lintcode(638)Strings Homomorphism
- LintCode之Strings Homomorphism
- LintCode python 小白-简单题-638-Strings Homomorphism
- Lie group homomorphism
- Strings
- Strings
- strings
- Strings
- Strings
- Strings
- strings
- Strings
- 图同构(graph isomorphism)和图同态(graph homomorphism)
- Foundation - strings
- java异常机制分析
- Nginx搭建
- Sony Xperia Z3+ E6553 开启 VoLTE
- BZOJ 1568: [JSOI2008]Blue Mary开公司 标记永久化
- C3P0配置属性
- Strings Homomorphism
- <NOIP> 26 . P1478 陶陶摘苹果(升级版)
- 二叉搜索树的性质
- JavaSwing_2.2: JButton(按钮)
- LeetCode-415. Add Strings (Java)
- jquery.validate.js表单验证插件使用
- 华为——质数因子_
- Windows上使用压缩包安装MYSQL
- git新建本地库上传到远程库