给定两个字符串 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. Cts框架解析(6)-任务的运行

    前两篇讲了任务的加入和9大项配置,这篇讲任务的运行. 任务的运行 任务的运行在CommandScheduler的run方法中,所以删除全部的断点,在run方法中打上断点,重新启动启动debug: 先看 ...

  2. Navicat for MySQL中文破解版免费下载

    不知道用过MySQL workbench数据库的朋友或站长有没有都遇到过这样的问题? 就是用ssh的连接方式连接数据库的时候,第一次可以连接的上,但是第二次就不行了,以后再用workbench就再也连 ...

  3. debian repository的成长过程

    1 基本概念 1.1 健康的安装 在端系统中的一次健康的安装指的是,在安装的包的集合中,所有的依赖都满足,并且没有冲突存在. 这的健康的安装是相对于端系统而言的,并不是相对于整个repo而言的.对整个 ...

  4. Infrastructure for container projects.

    Linux Containers https://linuxcontainers.org/

  5. Delphi的goto语法

    今天第一次主要到Delphi也有goto语法,特别是其奇怪的label声明.估计主要是用来跳出多重循环,而且还真有人使用这种方式.记个笔记: procedure TForm1.btn3Click(Se ...

  6. 有关使用HTTP协议传输二进制文件

    HTTP协议是基于字符(ASCII)的,当Content-Type项为text/xml,则内容是文本格式:当二进制格式时,Content-Type项为image/gif,就是了.例如,浏览器请求一张图 ...

  7. AOP 基本术语及其在 Spring 中的实现

    无论是 Spring 还是其他支持 AOP(Aspect Oriented Programming)的框架,尤其是 Spring 这种基于 Java(彻底的面向对象)的语言,在实现 AOP 时,首先为 ...

  8. CodeForces-652D:Nested Segments(树状数组+离散化)

    You are given n segments on a line. There are no ends of some segments that coincide. For each segme ...

  9. hdu 5289(单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  10. bzoj1138

    dp+spfa优化 最朴素的dp是dp[i][j]表示i->j的最短路,然后把所有pair(i,i)放到队列里跑spfa,但是这样被卡掉了,那么我们要优化一下 问题在于每次我们转移的时候要枚举i ...