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. luogu2714 四元组统计 莫比乌斯反演 组合数

    题目大意 给出一段序列,求其中最大公约数为1的四元组的个数. 思路 我们要用到反演.正难则反的思想.对于每一个大于1的数字\(x\),求出最大公约数为\(x\)的四元组的个数\(g(x)\),然后用排 ...

  2. POJ3204 Ikki's Story - Road Reconstruction 网络流图的关键割边

    题目大意:一个有源有汇的城市,问最少增加城市中的多少道路可以增加源到汇上各个路径上可容纳的总车流量增加. 网络流关键割边集合指如果该边的容量增加,整个网络流图中的任意从原点到汇点的路径的流量便可增加. ...

  3. 9.14[XJOI] NOIP训练33

    今日9.14 洛谷打卡:大凶!!!(换个字体玩玩qwq) -------------------------------------------------------- 一个超颓的上午 今天又是fl ...

  4. Ajax+Struts做登录判断

    Action类里: /* * 登录 */ public ActionForward doLogin(ActionMapping mapping,ActionForm form,HttpServletR ...

  5. 5.Project常用操作介绍

    Project常用操作介绍 1.项目浏览器 2.项目属性 Name:项目名称 Category:项目组织结构 Author:作者 Copyright:版权 Image:项目图标 Description ...

  6. vue-留言板-bootstrap

    最近看完入门API,看完视频自己写了个留言板,因为主要是学习vue,所以就复习了一下bootstrap,布局更简单,先看看样式吧. 简单清晰的布局,先说一下功能, 1.输入用户名密码点击提交放入表格 ...

  7. 【Oracle】闪回drop后的表

    本文介绍的闪回方式只适用于:删除表的表空间非system,drop语句中没有purge关键字(以上两种情况的误删除操作只能通过日志找回): 1.删除表后直接从回收站闪回 SCOTT@LGR> d ...

  8. LINUX 环境安装 jdk-tomcat安装

    linux版本两种安装方式 卸载自带jdk $rpm -qa | grep java $ rpm -e --nodeps java-**-openjdk-*$ rpm -e --nodeps java ...

  9. 多态&接口

    多态 多态定义:允许一个父类变量引用子类的对象:允许一个接口类型引用实现类对象. 多态的调用:使用父类的变量指向子类的对象:所调用的属性和方法只限定父类中定义的属性和方法,不能调用子类中特有的属性和方 ...

  10. (转)基于MVC4+EasyUI的Web开发框架形成之旅--MVC控制器的设计

    http://www.cnblogs.com/wuhuacong/p/3284628.html 自从上篇<基于MVC4+EasyUI的Web开发框架形成之旅--总体介绍>总体性的概括,得到 ...