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 ...
随机推荐
- css中!important的用法
{*rule !important}这个css规则当今在网页制作的时候的普及已经非常流行了,以前我对它的理解就停留在‘浏览器是否识别阶段’ 而没有真正去研究过,可是现在发生了变化.众所周知,!impo ...
- quartz + spring 启动项目时,报错The web application [] appears to have started a thread named.........
只是想记录自己的错误信息,下次再出现就知道怎么操作,不用再查找资料 解决办法: package com.wqq.quartz_test.schedule; import javax.servlet.S ...
- DB2大数据量优化查询解决方案
利用DB2表分区的功能对大数据量的表进行分区,可以优化查询. 表分区介绍: 表分区是一种数据组织方案,它根据一列或多列中的值把表数据划分为多个称为数据分区 的存储对象. (我觉得表分区就类似于Wind ...
- Educational Codeforces Round 33
# Who = Penalty * A B C D E F 479 arkethos 4 247 + 00:08 + 00:19 +1 00:59 +2 01:41 479 ne-leo ...
- Java基础9一面向对象
继承 1.特点 a) 要有一定的层次结构,并且具备可传递性. b) 判断两者之间是否有继承关系通过is-a来判断. c) 子类继承了父类,那么子类就继承了父类中所有的属性和方法,但是父类中的私有属性和 ...
- js---通过arguments来获取指定参数
通过访问arguments对象的length属性可以获取有多少个参数传递给了函数. 如:每次被调用的时候,输出传入其中的参数个数 function doAdd(){ alert(arguments.l ...
- 实验7 OpenGL光照
一.实验目的: 了解掌握OpenGL程序的光照与材质,能正确使用光源与材质函数设置所需的绘制效果. 二.实验内容: (1)下载并运行Nate Robin教学程序包中的lightmaterial程序,试 ...
- 配置notepad++编程环境
1. 到 https://sourceforge.net/projects/mingw-w64/files/ 下载MinGW64,解压并移动到C盘根目录 2. 将 C:\MinGW64\bin 加入系 ...
- 应运而生! 双11当天处理数据5PB—HiStore助力打造全球最大列存储数据库
阿里巴巴电商业务中历史数据存储与查询相关业务, 大量采用基于列存储技术的HiStore数据库,双11当天HiStore引擎处理数据记录超过6万亿条.原始存储数据量超过5PB.从单日数据处理量上看,该系 ...
- day02_20190106 基础数据类型 编码 运算符
一.格式化输出 name = input('请输入姓名') age = input('请输入年龄') hobby = input('请输入爱好') job = input('请输入你的工作') # m ...