isIsomorphic
2015-06-03 00:18
169 查看
超时版:
AC
/* 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. Note: You may assume both s and t have the same length.*/ import java.util.*; public class Isomorphic_strings { public static void main(String[] args) // TODO Auto-generated method stub { System.out.println(isIsomorphic("papera", "titlei")); /* * String str="dauqka"; String str1=str.replace('a','x'); str=str1; * System.out.println(str); */ } public static boolean isIsomorphic(String s, String t) { int x = 0; if (s.length() != t.length()) return false; char[] s_chs = s.toCharArray(); char[] t_chs = t.toCharArray(); for (int i = 0; i < s_chs.length; i++) { if (!(s_chs[i] == t_chs[i])) { for (int j = 0; j < i; j++) { if ((t_chs[j] == t_chs[i])) x = 1; } } if (x == 0) t = t.replace(t_chs[i], s_chs[i]); if (x == 1) { t_chs[i] = s_chs[i]; t = String.valueOf(t_chs); } t_chs = t.toCharArray(); } return (s.equals(t)); } }
AC
public static boolean isIsomorphic(String s, String t) { Map<Character, Character> hm = new HashMap<Character, Character>(); if (s.length() != t.length()) return false; char[] s1 = s.toCharArray(); char[] t1 = t.toCharArray(); for (int i = 0; i < s1.length; i++) { if (hm.containsKey(s1[i])) { if ((t1[i] != hm.get(s1[i]))) return false; } hm.put(s1[i], t1[i]); } return true; } }
相关文章推荐
- Python包RLPY初识(一)
- javascript-学习笔记-<script> </script> 代码执行的和上一个博客相对应的
- 帆布指纹识别——用户无隐私
- 05-图2. Saving James Bond - Easy Version (25)
- 汉罗塔lua实现
- android屏幕适配建议
- [LeetCode] Best Time to Buy and Sell Stock II
- PCIe学习记录
- java jdk查看源码
- 数据结构基础
- [LeetCode] Best Time to Buy and Sell Stock
- Java反射技术一:基本知识点
- hdu 1789 排序+数组标记
- html页面元素加载顺序
- REST聊天应用升级——妖哲开始打通后端6
- 基于TCP的通信 客户端
- poj 2480 Longge's problem 积性函数性质+欧拉函数
- B/S模式实现批量打包apk
- OpenLayers学习笔记2——坐标转换问题
- 基于v4l2的摄像头驱动笔记