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 ...
随机推荐
- mysql的text的类型注意
不要以为text就只有一种类型! Text也分为四种类型:TINYTEXT.TEXT.MEDIUMTEXT和LONGTEXT 其中 TINYTEXT 256 bytes TEXT 65,535 byt ...
- WCF学习心得------(三)配置服务
配置服务 配置服务概述 在设计和实现服务协定后,便可以进行服务的配置.在其中可以定义和自定义如何向客户段公开服务,包括指定可以找到服务的地址,服务用于发送和接受消息的传输和消息编码,以及服务需要的安全 ...
- 【控件扩展】带圆角、边框、渐变的panel
下载地址: http://files.cnblogs.com/chengulv/custompanel_demo.zip using System; namespace LC.Fun { /// & ...
- C# winform 右下角弹出窗口结果
using System.Runtime.InteropServices; [DllImport("user32")] private static extern bool Ani ...
- 剑指offer系列50--不用加减乘除做加法
[题目]写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号 * [思路]1 不计进位,直接位运算(异或方式可实现此运算,即1+0 0+1为1,0+0 1+1位0) * 2 与 ...
- gdb: multiple process debug
gdbserver自身不支持multiple process:如果你调试parent process时在子进程上下断点,子进程在运行到那个断点时就会SIGTRAP. 如果你要调试fork出来的子进程: ...
- 在eclipse中设计BPMN 2.0工作流定义的根本步骤
原文地址:http://www.myexception.cn/eclipse/1863140.html 在eclipse中设计BPMN 2.0工作流定义的基本步骤 1. Activiti问我们提供了A ...
- AngularJS PhoneCat代码分析
转载自:http://www.tuicool.com/articles/ym6Jfen AngularJS 官方网站提供了一个用于学习的示例项目:PhoneCat.这是一个Web应用,用户可以浏览一些 ...
- 2. scala中的数组
一. 数组声明 定长数组 scala> new Array[String](5) res1: Array[String] = Array(null, null, null, null, null ...
- Tomcat DEBUG模式下修改代码立刻生效!