给定两个字符串 s 和 t,判断它们是否是同构的。
如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的。
所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
例如,
给定 "egg", "add", 返回 true.
给定 "foo", "bar", 返回 false.
给定 "paper", "title", 返回 true.

详见:https://leetcode.com/problems/isomorphic-strings/description/

Java实现:

class Solution {
public boolean isIsomorphic(String s, String t) {
if(s.length()!=t.length()){
return false;
}
int[] hash1=new int[256];
int[] hash2=new int[256];
for(int i=0;i<s.length();++i){
if(hash1[s.charAt(i)]!=hash2[t.charAt(i)]){
return false;
}
hash1[s.charAt(i)]=i+1;
hash2[t.charAt(i)]=i+1;
}
return true;
}
}

C++实现:

class Solution {
public:
bool isIsomorphic(string s, string t) {
int m1[256]={0},m2[256]={0};
for(int i=0;i<s.size();++i)
{
if(m1[s[i]]!=m2[t[i]])
{
return false;
}
m1[s[i]]=i+1;
m2[t[i]]=i+1;
}
return true;
}
};

参考:https://www.cnblogs.com/grandyang/p/4465779.html

205 Isomorphic Strings 同构字符串的更多相关文章

  1. [leetcode]205. Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  2. [leetcode]205. Isomorphic Strings同构字符串

    哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查 ...

  3. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  4. 205. Isomorphic Strings - LeetCode

    Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...

  5. 【刷题-LeetCode】205. Isomorphic Strings

    Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...

  6. LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

    翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...

  7. LeetCode 205. Isomorphic Strings (同构字符串)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. Leetcode 205 Isomorphic Strings 字符串处理

    判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...

  9. [LeetCode] 205. Isomorphic Strings 解题思路 - Java

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

随机推荐

  1. HTTP协议漫谈 C#实现图(Graph) C#实现二叉查找树 浅谈进程同步和互斥的概念 C#实现平衡多路查找树(B树)

    HTTP协议漫谈   简介 园子里已经有不少介绍HTTP的的好文章.对HTTP的一些细节介绍的比较好,所以本篇文章不会对HTTP的细节进行深究,而是从够高和更结构化的角度将HTTP协议的元素进行分类讲 ...

  2. Eclipse:Some sites could not be found. See the error log for more detail.解决的方法

    今天遇到了一个奇葩的问题.我把我的sdk tools的版本号升级到23后.我在eclipse中尝试升级ADT,发现了这么一个问题,以下分析下原因: 当我在eclipse中选择Help-->Che ...

  3. 2016/04/18 session cookie 对比 应用 <?php session_start() / setcookie()?>

    ①会话 huihua.php <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  4. Lightoj 1166 - Old Sorting

    Given an array containing a permutation of 1 to n, you have to find the minimum number of swaps to s ...

  5. leetcode 660. Remove 9

    Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...

  6. jquery中的工具函数 Utilities

    noConflict(deep) 释放$和Jquery的控制权 isFunction(obj) isArray(obj) isWindow(obj) isNumeric(obj) type(obj) ...

  7. 什么叫强类型的DATASET

    强类型DataSet,是指需要预先定义对应表的各个字段的属性和取值方式的数据集.对于所有这些属性都需要从DataSet, DataTable, DataRow继承,生成相应的用户自定义类.强类型的一个 ...

  8. redhat修复hostname主机名

    1.修改文件vi /etc/sysconfig/network下的hostname,如: NETWORKING=yes HOSTNAME=master 2.修改文件:vi /etc/hosts 127 ...

  9. 源代码管理工具GIT

    01.GIT简介 svn是集中式的源代码管理工具,必须联网才能操作 git是分布式的. 有两中:一个是本地代码仓库,一个是远程代码仓库 分布式源代码管理工具 02.GIT - 本地代码仓库使用流程 1 ...

  10. NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推 ||卡特兰数(转化成01字符串))

    Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m. ...