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 ...
随机推荐
- js和css内联外联注意事项
简单说:这两个问题其实是同一个问题,但是网上找了好久也找不到方法,外联的js和css文件里不能有任何HTML的标记注释,一旦有,浏览器就疯了!一去掉就好了!!! 问题:起因是网上看到一个css的表格样 ...
- select跳转
<select onchange="window.open(this.options[this.selectedIndex].value)"><option> ...
- 配置Windows Server2008+iis+php+mysql所需下载安装包
最近一个朋友让我帮忙给配置服务器iis+php+mysq 环境,遇到了很多问题,特此就在这里说一下.小弟只是在windwos2003 和windwos XP下配置过iis+php+mysql,去朋友那 ...
- 【python】filter,map,reduce和lambda函数介绍
filter(function, iterable)map(function, iterable)reduce(function, sequence) filter将 function依次作用于ite ...
- 剑指offer系列43---判断平衡二叉树
[题目]判断一颗二叉树是不是平衡二叉树. * 平衡二叉树定义:任意子节点深度相差不超过1.[思路]由上题,利用递归得到二叉树每个结点的深度同时比较. package com.exe9.offer; i ...
- 【转】 SQL 2005 try catch
1 TRY…CATCH 1.1 用法 TRY…CATCH的语法如下: BEGIN TRY -- TRY 模块 -- 业务处理 END TRY BEGIN CATCH -- CATC ...
- PLSQL_性能优化系列11_Oracle Bulk Collect批处理
2014-10-04 Created By BaoXinjian
- CSU 1803 2016(数论)
2016 Problem Description: 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1≤a≤n,1≤b≤m; a×b 是 2016 的倍数. Input: 输 ...
- NeHe OpenGL教程 第四十课:绳子的模拟
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- Linux命令(18)查看当前用户who、whoami、who am i
首先看命令的使用情况: [@sjs_9_108 ~]$ whoami spider [@sjs_9_108 ~]$ who am i spider pts/ -- : (192.168.1.1) [@ ...