isIsomorphic
超时版:
/*
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;
} }
isIsomorphic的更多相关文章
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- 全部leetcode题目解答(不含带锁)
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.) 88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- 【leetcode】Isomorphic Strings
题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- leetcode 205
205. Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are ...
- Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- isa class 帮助确定对象或变量的数据类型
isa class 帮助确定对象或变量的数据类型
- 剑指offer系列56---连续子数组的最大和
[题目]输入一个整型数组,数组里有正数也有负数.数组中一个或连续多个整数组成一个子数组. * 求所有子数组和的最大值. * [思路]连续求和数组元素.一旦得到的和小于0,就抛弃前面的数组,从当前值重写 ...
- 剑指offer系列28--字符流中第一个不重复的字符
[题目]请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符”go”时,第一个只出现一次的字符是”g”.当从该字符流中读出前六个字符“google”时,第一个只出现 ...
- (转)用Eclipse进行C++开发时Bianry not found的问题解决
本文转载自:http://blog.csdn.net/baimafujinji/article/details/49722399 由于Visual Studio体积过于庞大,很多人选择在Eclipse ...
- bzoj4709 [jsoi2011]柠檬
Description Flute 很喜欢柠檬.它准备了一串用树枝串起来的贝壳,打算用一种魔法把贝壳变成柠檬.贝壳一共有 N (1 ≤ N ≤ 100,000) 只,按顺序串在树枝上.为了方便,我们 ...
- idea系列新版注册模式
http://idea.qinxi1992.cn/ 楼上被列入黑名单,用 http://114.215.133.70:41017/
- Linux中文件描述符fd和文件指针flip的理解
转自:http://www.cnblogs.com/Jezze/archive/2011/12/23/2299861.html 简单归纳:fd只是一个整数,在open时产生.起到一个索引的作用,进程通 ...
- mssql查询某个值存在某个表里的哪个字段的值里面
第一步:创建 查询某个值存在某个表里的哪个字段的值里面 的存储过程 create proc spFind_Column_In_DB ( @type int,--类型:1为文字类型.2为数值类型 )-- ...
- 黄聪:wordpress源码解析-目录结构-文件调用关系(转)
Wordpress是一个单入口的文件,所有的前端处理都必须经过index.php,这是通过修改web服务器的rewrite规则来实现的.这种做法的好处是显而易见的,这样URL更好看,不必为每一个url ...
- Redis数据库?-Redis的Virtual Memory介绍(转)
众所周知,Redis是一个内存数据库,和Memcached类似,所有数据存在内存中,当然,Redis有rdb和appendonlyfile两个落地文件,可以对断电停机等故障下的数据恢复做一些保证.但是 ...