[LinkedIn]Isomorphic Strings Dictionary substitution
来源:互联网 发布:mac版围棋对弈软件 编辑:程序博客网 时间:2024/06/16 18:59
From Here
Given two (dictionary) words as Strings, determine if they are isomorphic.
Two words are called isomorphic if the letters in one word can be remapped to get the second word. Remapping a letter means replacing all occurrences of it with another letter while the ordering of the letters remains unchanged. No two letters may map to the same letter, but a letter may map to itself.
Wrong Solution:
map char to char, then check in the hashmap. However, this will not work!
since no two letters may map to the same letter. This will damage the rule
Best solution:
HashMap (char, firstSeenIndice) for each string. The encoding of firstSeenIndices should match.
E.g. Foo and app both encode to 011
Abcd and hole both encode to 0123
public boolean isomorphic(String s, String t) { if (s.length() != t.length()) { return false; } return (sequence(s).equals(sequence(t)));}private String sequence(String s) { HashMap<Character, Integer> map = new HashMap<Character, Integer>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { if (map.containsKey(s.charAt(i))) { sb.append(map.get(s.charAt(i))); } else { map.put(s.charAt(i), i); } } return sb.toString();}
- [LinkedIn]Isomorphic Strings Dictionary substitution
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- 中国银行支付接口(ecshop版)
- Arm Reference
- RabbitMQ消息队列(二):”Hello, World“(转)
- potplayer打开多个视频文件
- 提示 Available Memory Is Low
- [LinkedIn]Isomorphic Strings Dictionary substitution
- javaee学习之路(二十一)JavaWeb项目实战--银行存款业务
- 查找1亿个数里面最大的100个数。
- 基于matlab的彩色图像点阵提取小工具
- RabbitMQ消息队列(三):任务分发机制(转)
- 550 failed to change directory
- linux 登陆远程mysql (指定端口号)
- logback系列之四:输出日志到不同文件
- php 分页总结