给定两个字符串 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. Office EXCEL 复制粘贴 变成 #value,#REF!,#DIV怎么办

    这些都是由于相对引用造成的,如下所示,我鼠标点进去之后变成了I10/L10,当数字和文字或空单元格进行加减乘除的运算就会出现这种问题   使用选择性粘贴,只粘贴数值即可.

  2. org.json.JSONException: A JSONObject text must begin with &#39;{&#39; at character 1 of {解决方法

    在使用java读取一个本地的json配置文件的时候,产生了这个异常:org.json.JSONException: A JSONObject text must begin with '{' at c ...

  3. Java设计模式-设计模式的六种原则

    所谓无招胜有招,练一门功夫分为内功和外功. 外功好比招式,就是所谓的23种设计模式.而内功呢,就是心法,那就是这6种法则.光会外功那是花拳绣腿,内功修为才是境地. 如此众多的设计模式,学完2遍.3遍可 ...

  4. Servlet访问Javabean并传结果给jsp

    1.先建立包名: 2.建立实体类 参考二维表,考虑各个字段名字.类型 在entity包里面建立一个类,代码如下: public class House { private String id; pri ...

  5. 使用 C# 开发智能手机软件:推箱子(三)

    这是"使用 C# 开发智能手机软件:推箱子"系列文章的第三篇.在这篇文章中,介绍 Common/Block.cs 源程序文件.   1 namespace Skyiv.Ben.Pu ...

  6. Delphi中accesss实现树形结构查询系统(一次性生成比较方便)

    主要是要读取数据库的信息,而delphi界面是一个树形结构. 例如有一个Ascess数据库:示例.MDB,内有一张表:“国家”,表的内容如下: 编号        名称  01             ...

  7. beego5---gosqlite安装

    WindowsWindows下的安装也非常简单,只要到 SQLite3 的下载页面,下载 Windows 下的预编译包 DLL 的压缩包(sqlite-dll-win32-x86-XXX.zip),然 ...

  8. VisualSVN Server的配置和使用

    VisualSVN Server的配置与使用 本版本为VisualSVN Server 2.7.3版本-不同的版本可能在设置有不同的差异,但都大同小异 1.1启动界面 安装好 VisualSVN Se ...

  9. Windows7下安装golang语言开发环境和revel框架

    1.下载先去下载32位或64 golang window 安装包 并安装下载地址:https://www.golangtc.com/download 本人更改了安装地址为 D:\GO\Go 2. go ...

  10. python3中urllib的基本使用

    urllib 在python3中,urllib和urllib2进行了合并,现在只有一个urllib模块,urllib和urllib2的中的内容整合进了urllib.request,urlparse整合 ...