leetcode 205
205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.
Note:
You may assume both s and t have the same length.
设定两个字符串长度相等,所谓的“同构”,就是字符串 s 中的字符可以一对一的映射到字符串 t 中的字符。不能一对多,也不能多对一。
利用两个map实现。
代码如下:
class Solution {
public:
bool isIsomorphic(string s, string t) {
map<char, char> mapA;
map<char, char> mapB;
int n = s.length();
for(int i = ; i < n; i++)
{
char ss = s[i];
char tt = t[i];
map<char, char>::iterator it = mapA.find(ss);
if(it != mapA.end())
{
if(mapA[ss] != tt)
{
return false;
}
}
else
{
map<char, char>::iterator ii = mapB.find(tt);
if(ii != mapB.end() && mapB[tt] != ss)
{
return false;
}
else
{
mapA[ss] = tt;
mapB[tt] = ss;
}
}
}
return true;
}
};
leetcode 205的更多相关文章
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- LeetCode 205. Isomorphic Strings (同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- Java实现 LeetCode 205 同构字符串
205. 同构字符串 给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序. ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Leetcode 205 Isomorphic Strings 字符串处理
判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...
- (easy)LeetCode 205.Reverse Linked List
Reverse a singly linked list. 解法一:记录单链表每个节点的val,然后重新为单链表赋值.(取巧,仅仅是将val部分改变,原始node节点并没有改变) 代码如下: /** ...
- (easy)LeetCode 205.Isomorphic Strings (*)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Java [Leetcode 205]Isomorphic Strings
题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
随机推荐
- windows server 2012 FTP SMB 文件夹权限继承
被坑了..win默认的权限继承,有继承就没有smb目录继承,有smb目录继承 父级文件夹的权限就删不掉.. 换ftp轻松愉快...
- Network Alignment(网络比对)模型
两类模型: 第一类:two-steps method 先计算两个网络之间每两个结点的相似性,再从N1*N2对相似性中抽取N1对匹配(N1<=N2). 代表算法:IsoRank 第二类:obj ...
- yii2史上最简单式安装教程,没有之一
写一篇绝对堪称史上最easy的Yii2安装教程教你入门. 既然是安装Yii,我们先去官网下载一份Yii的高级模版,什么,你说打开页面乱七八糟的英文字母你看不懂?那这样大哥,你按照下面的截图进行操作好吧 ...
- JS转换HTML转义符
JS转换HTML转义符 //去掉html标签 function removeHtmlTab(tab) { return tab.replace(/<[^<>]+?>/g,'') ...
- Delphi编程获取系统当前进程、窗口句柄、文件属性以(转)
Delphi编程获取系统当前进程.窗口句柄.文件属性以及程序运行状态. uses TLHelp32,PsAPI; (1)显示进程列表:procedure TForm1.Button2Click(Sen ...
- 【zz】MIT牛人解说数学体系
作者:林达华 一.为什么要深入数学的世界 作为计算机的学生,我(原作者)没有任何企图要成为一个数学家.我学习数学的目 的,是要想爬上巨人的肩膀,希望站在更高的高度,能把我自己研究的东西看得更深广一些. ...
- javascript判断某种元素是否进入可视区域
判断是否在指定的可视区域内,先用最简单的方式,比如整个页面为可视区域 找到几个关键因素: sTop= $(window).scrollTop(); //滚动条距顶部的高度 clientHeight= ...
- EventBus--出现的问题
--- 1 , EventBus收不到消息问题. 项目中遇到的问题,做搜索商品的时候遇到, 1.情况是一个FragmentActivity包含四个碎片Fragment,在FragmentActivit ...
- C#封装好的Win32API
Kernel.cs using System; using System.Runtime.InteropServices; using System.Text; using HANDLE = Syst ...
- 使用Junit对Spring进行单元测试实战小结
Demo代码: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:/ ...