leetcode_Isomorphic Strings _easy
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.
方法:使用2个map就可以。
class Solution {
public:
bool isIsomorphic(string s, string t) {
map<char,char> ccmap,ccmap2;
int i=s.length(),j=t.length();
if(i==j)
{
for(int m=0; m<i; m++)
{
map<char,char>::iterator iter=ccmap.find(s[m]);
if(iter!=ccmap.end())//找到。此字符之前已经作为原始字符在替换中用过
{
if((*iter).second!=t[m])
return false;
}
else
ccmap[s[m]]=t[m];
}
//通过map推断是否存在那种aa, ba这种情况导致的多个字符映射到同一个字符的错误。假设存在,那么返回false
int len=ccmap.size(),len2;
//将ccmap中的key和value进行调换位置赋值给ccmap2,ccma自己不变
for(map<char,char>::iterator iter=ccmap.begin(); iter!=ccmap.end(); iter++)
ccmap2[(*iter).second]=(*iter).first;
len2=ccmap2.size();
if(len!=len2)//长度不等。那么说明ccmap中存在value相等的元素(如aa,ba: a-->b, a-->a,false)
return false;//
return true;
}
return false;
}
};
leetcode_Isomorphic Strings _easy的更多相关文章
- LeetCode_Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [LeetCode] Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
随机推荐
- Android——build.prop 解析【转】
本文转载自:http://blog.csdn.net/lengyue1084/article/details/77637354 一.概念 在Android设备shell终端可以看到/system目录下 ...
- WebRequestSugar
用法 WebRequestSugar ws = new WebRequestSugar(); //可选参数 //ws.SetAccept //ws.SetContentType //ws.SetCoo ...
- Android平台下的TCP/IP传输(客户端)
在工科类项目中,嵌入式系统与软件系统或后台数据库之间的信息传输是实现“物联网”的一种必要的途径,对已简单概念的物联网,通常形式都是一个单片机/嵌入式系统实现数据的采集及其处理,通过蓝牙,wifi或者是 ...
- BZOJ 2729 高精度+组合数学
思路: 考虑 把男生排成一排 女生和老师往里插 分成两种情况. 1. 女生中间夹着老师 2. 女生中间没有夹着老师 求一下组合* 阶乘就好了 先放Python代码 简洁易懂 def fact(n): ...
- 单元测试之Mock
为什么需要Mock. 真实对象具有不确定的行为.所以会产生不可预测的结果. 真实对象很难被创建. 真实对象的某些行为很难被触发(如网络错误). 真实对象令程序的运行速度很慢. 真实对象有(或者是)用户 ...
- IE浏览器缓存导致Ajax请求失败
在IE浏览器中通过Ajax请求后台的数据,如果Page请求是postback类型的,可能会导致Ajax请求失败的问题 我们都知道ajax能提高页面载入的速度主要的原因是通过ajax减少了重复数据的载入 ...
- 前端-JS思维导图
看不清的朋友右键保存或者新窗口打开哦!喜欢我可以关注我,还有更多前端思维导图笔记
- Matplotlib库常用函数大全
Python之Matplotlib库常用函数大全(含注释) plt.savefig(‘test’, dpi = 600) :将绘制的图画保存成png格式,命名为 test plt.ylabel(‘Gr ...
- JAVA代理方式使用示例总结
JAVA代理方式使用示例总结 一. 代理方式概括 Java的代理方式主要包含了静态代理,动态代理两种方式,其中,动态代理根据实现的方式不同,又可以划分为jdk动态代理和cglib动态代理. 二. ...
- 使用Eric构建Caffe应用程序-Baby年龄识别
训练好的Caffe网络结构,可以固定下来,直接载入程序作为数据库接口使用.本文使用Eric构建运行于Python环境下的图片识别应用程序,因为Eric使用QT作为GUI,且有Python的接口,可直接 ...