LeetCode Isomorphic Strings 对称字符串

题意:如果两个字符串是对称的,就返回true。对称就是将串1中的同一字符都一起换掉,可以换成同串2一样的。
思路:ASCII码表哈希就行了。需要扫3次字符串,共3*n的计算量。复杂度O(n)。从串左开始扫,若字符没有出现过,则赋予其一个特定编号,在哈希表中记录,并将该字符改成编号。对串2同样处理。其实扫2次就行了,但是为了短码。
class Solution {
public:
void cal(string &s)
{
int has[]={}, cnt=;
for(int i=; i<s.size(); i++)
{
if(!has[s[i]])
has[s[i]]=++cnt;
s[i]=has[s[i]];
}
}
bool isIsomorphic(string s, string t) {
if(s.size()!=t.size()) return false;
if(s.size()==) return true;
cal(s);
cal(t);
for(int i=; i<s.size(); i++)
if(s[i]!=t[i]) return false;
return true;
}
};
AC代码
LeetCode Isomorphic Strings 对称字符串的更多相关文章
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [LeetCode] Isomorphic Strings
Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...
- [leetcode]205. Isomorphic Strings同构字符串
哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查 ...
- [LeetCode] Buddy Strings 伙计字符串
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- Python3解leetcode Isomorphic Strings
问题描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- 205 Isomorphic Strings 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...
- 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)
题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...
- CodeForces985F:Isomorphic Strings (字符串&hash)
题意:取出字符串Str里的两个串S,T,问对应位置的的字符在否有一一映射关系. hash:对于每个字符s=‘a’-‘z’,我们任意找一个i,满足Si==s,(代码里用lower_bound在区间找到最 ...
随机推荐
- Firebug的安装与使用
第一步,点击 Firefox 浏览器上的“工具”选项,然后点击“附加软件”,在弹出的小窗口中,点击右下角的“获取扩展”选项,如图 4 所示. 图 4. 获取扩展 第二步,在点击“获取扩展”选项后,打开 ...
- matlab 2017版本修改帮助文档为离线
安装matlab2017a后发现,帮助文档为在线版本,必须关联账号. 经过查询资料,帮助文档默认是使用web, on mathworks.com 可以将帮助文档改为离线版本. 具体修改方法: Pref ...
- A. Launch of Collider (#363 Div.2)
A. Launch of Collider time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- ElasticSearch基础之查询功能
[01]查询类型: [02]基本查询和组合查询是参与打分的 1.创建映射: 注意事项:基于上面映射的创建: "type": "keyword" # 如果某个字段 ...
- 【网络爬虫】【python】网络爬虫(二):网易微博爬虫软件开发实例(附软件源码)
对于urllib2的学习,这里先推荐一个教程<IronPython In Action>,上面有很多简明例子,并且也有很详尽的原理解释:http://www.voidspace.org.u ...
- ASP.NET Core快速入门_学习笔记汇总
第2章 配置管理 任务12:Bind读取配置到C#实例 任务13:在Core Mvc中使用Options 任务14:配置的热更新 任务15:配置框架设计浅析 第3章 依赖注入 任务16:介绍- 任务1 ...
- war,jar包是啥
http://www.blogjava.net/athrunwang/archive/2011/11/18/364191.html 经常听开发说war,jar,car,这些是个什么东东呢? .jar ...
- mock api
模客:http://mock-api.com/ easy-mock:https://www.easy-mock.com/ easy-mock动不动就挂了,而且用起来特别卡,不知道为什么那么多人推荐-_ ...
- css 3d旋转
- 网络工程师HCNA认证学习笔记Day1
企业网络 企业网络远程互联是广域网WAN互联,而非互联网Internet小型企业网络:一个路由器.交换机.AP大型企业网络:核心层.汇聚层.接入层.考虑可用性.稳定性.扩展性.安全性.可管理,冗余. ...