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的更多相关文章

  1. LeetCode_Isomorphic Strings

    Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...

  2. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  3. 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 ...

  4. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  6. [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 ...

  7. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  8. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  9. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

随机推荐

  1. TCP三次握手及关闭时的2MSL分析

    TCP/IP三次握手四次挥手,是非常重要的,这个链接与关闭过程也是非常easy的.但为什么是三次握手?以及为什么要等待2MSL的状态?大部分人或许听到这个问题就蒙了.这篇博客就综合<TCP/IP ...

  2. 【SDOI 2010】 计算器

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2242 [算法] 第一问用快速幂解决 第二问用exgcd解决 第三问用BSGS算法解决 ...

  3. c# xml操作总结

    一前言 先来了解下操作XML所涉及到的几个类及之间的关系  如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家 * 1 XMLElement 主要是针对节点的一些属性进行操作 ...

  4. JS——BOM操作(点击按钮返回顶部案例:scrollTop的用法)

    点击按钮返回顶部案例: 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  5. Docker 内部之间的网络连接

    一.简介 内部网络连接的2中方式: Docker NetWorking (1.9版本之后推荐使用这个)和 Docker link(1.9 版本之前都使用这个) 推荐使用docker networkin ...

  6. 关于KO信息

    最近写大论文查到KO也是可以用于分类的一种信息. 如何使用KEGG进行通路富集http://blog.sciencenet.cn/blog-364884-779116.html kegg 数据库学习笔 ...

  7. java控制台输入输出字符串

    一.实例说明 本实例通过输入流(System.in)实现从控制台接受用户输入信息,并将该信息输出到控制台. 运行效果如下图: 二.实现代码 三.要点说明 该实例的关键就是用到了System类的输入流, ...

  8. Ubuntu14.04引导菜单修复

    原文链接:http://www.metsky.com/archives/636.html 独立分区下的Ubuntu引导菜单修复有点麻烦,执行挂载等命令时要小心检查,修复此类引导,首先需要确保当前系统和 ...

  9. Swift语法3.03(类型Types)

    类型 在Swift中,有两种类型:命名型类型和复合型类型.命名型类型是在定义时可以给定的特定名字的类型.命名型类型包括类,结构体,枚举和协议.例如,自定义的类MyClass的实例拥有类型MyClass ...

  10. ZBrush中如何将一个模型应用在不同的图层

    我们经常会使用ZBrush®中的插入笔刷来实现快速建模,或者使用Insert笔刷创建人物四肢,那么在使用这些笔刷时,它默认是和所接触模型同在一个Subtool,如果您需要不同的材质或者雕刻手法,那么就 ...