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 ...
随机推荐
- java中的循环
while循环: while(循环条件){ 循环操作 } 停止while循环有两种方式:1.不再满足while后的循环条件时,循环终止: ...
- 【Java编程进阶-1】enum枚举的使用
枚举主要用于枚举常量,下面举个简单的应用. 比如一个公司有如下几个部门: 研发部: 销售部: 财务部: (其他部门暂时不列举) 部门的某些信息相对固定,此时可以考虑使用枚举来说明: 枚举类 Depts ...
- C#读取系统信息
using System; using System.Management; namespace Soyee.Comm { /// <summary> /// Computer Infor ...
- 程序包com.sun.image.codec.jpeg不存在 问题的完美解决
原文地址:http://my.oschina.net/zb0423/blog/86507 在使用Hudson进行打包的过程中,因为我们使用了一个pdf文件产生缩略图的功能,倒置添加的源码文件在mave ...
- C# is和as操作符
is和as操作符 is操作符:检查对象是否与给定类型兼容. 说明: 1.如果所提供的表达式非空,并且所提供的对象可以强制转换为所提供的类型而不会导致引发异常,则 is 表达式的计算结果将是 true, ...
- Redis数据库?-Redis的Virtual Memory介绍(转)
众所周知,Redis是一个内存数据库,和Memcached类似,所有数据存在内存中,当然,Redis有rdb和appendonlyfile两个落地文件,可以对断电停机等故障下的数据恢复做一些保证.但是 ...
- DBA_Oracle Erp加密和解密账户密码(案例)
2014-09-09 Created By BaoXinjian
- CF 445A 简单DP
今天早上找一道题的bug,还是找不出来,下午刷了几道水题,晚上准备回家的事, 然后本来想打CF的,一看,数学场,不打了. 这道题的题意: 给出一个序列,每次你可以从这个序列里面选择一个数ak,删除,然 ...
- 发现木马C:\windows\system32\FastUserSwitchingCompatibilityex.dll
而且用安全狗还隔离不了
- android按钮监听器的四种技术
android开发中经常会用到各种各样的监听器,android监听器的写法与java又有不同的地方; 1,activity中使用内部类实现接口 ,创建内部类实例 使用add方法 与java类似 创 ...