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在区间找到最 ...
随机推荐
- g2o扩展,然后重新编译生成新库。
orb作者有g2o扩展,g2o原作者也有g2o扩展,等各项基本功扎实以后,考虑把他们整合在一起,再加上高博扩展的g2o,统一cmake,make,然后能make install 正常使用,就最好了.
- 洛谷1303 A*B Problem 解题报告
洛谷1303 A*B Problem 本题地址:http://www.luogu.org/problem/show?pid=1303 题目描述 求两数的积. 输入输出格式 输入格式: 两个数 输出格式 ...
- 删除链表中的倒数第n个元素
示例: 输入链表:1->2->3->4->5 , 2 输出:1->2->3->5 Python解决方案1: # Definition for singly-l ...
- POJ1088滑雪(记忆化搜索)
就是用DP,DP[i][j]是在这个(i,j)位置作为起点的最长长度. 因为可能会超时,DP的话每次就是记录,然后就不用回溯了. 很简单的DFS里面的记忆化搜索. #include <stdio ...
- 《C#入门经典》学习笔记(集合、比较和转换)
http://xiang-ai-2002.blog.163.com/blog/static/8477933201041824429161/ 集合 C#中的数组是作为System.Array类的实例来执 ...
- [Xcode 实际操作]八、网络与多线程-(11)使用同步Post方式查询IP地址信息
目录:[Swift]Xcode实际操作 本文将演示如何通过Post请求,同步获取IP地址信息. 一旦发送同步请求,程序将停止用户交互,直至服务器返回数据. 在项目导航区,打开视图控制器的代码文件[Vi ...
- ios代码大全
http://blog.csdn.net/kepoon/article/details/7763106
- 百度搜索:有关Baiduspider的10个问题
猫宁!!! 参考链接: http://help.baidu.com/question?prod_id=99&class=476&id=2996 https://ziyuan.baidu ...
- swipe轮播插件零基础实用
此篇博客整理了常用的轮播效果,适用于所有开发人员 swipe是当下相对而言较好用的轮播插件,下面是博主整理的demo源代码,可直接上手(备注:需自己手动swipe所需的j和css) 此段代码总共是有三 ...
- solr IK分词器
1.把IK文件夹上传到服务器tmp文件夹 2.把需要的jar导入到solr项目中 # cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps ...