博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode OJ:Isomorphic Strings(同构字符串)
阅读量:7037 次
发布时间:2019-06-28

本文共 1250 字,大约阅读时间需要 4 分钟。

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.

看两个字符串是否同构,用map就可以解决,不过要注意双向都要检查,代码如下:

1 class Solution { 2 public: 3     bool isIsomorphic(string s, string t) { 4         map
m1; 5 map
m2; 6 if(s.size() != t.size()) 7 return false; 8 for(int i = 0; i < s.size(); ++i){ 9 if(m1.find(s[i]) == m1.end() && m2.find(t[i]) == m2.end()){10 m1[s[i]] = t[i];11 m2[t[i]] = s[i];12 }13 else if(m1.find(s[i]) != m1.end() && m2.find(t[i]) != m2.end()){14 if(m1[s[i]] != t[i] || m2[t[i]] != s[i])15 return false;16 }else{17 return false;18 }19 }20 return true;21 }22 };

 

转载于:https://www.cnblogs.com/-wang-cheng/p/4987700.html

你可能感兴趣的文章
Maven项目中添加jFinal包以及源文件
查看>>
Android实用笔记——使用ViewPager实现导航
查看>>
深入理解Java虚拟机 读书笔记 之 how to STW
查看>>
有关数据库事务的一些理解
查看>>
MyEclipse Web Project转Eclipse Dynamic Web Project
查看>>
ELK之权限管理
查看>>
×_7_12_2013 I: Light on or off
查看>>
JIT
查看>>
巧用escalations限制Nagios报警次数 - [Nagios
查看>>
Entity SQL与LINQ TO Entity的本质区别
查看>>
python unittest 深入failfast及实际应用【示例】
查看>>
MSSQL中文排序规则设置
查看>>
30 个有关 Python 的小技巧
查看>>
CDN下nginx获取用户真实IP地址
查看>>
Jsp技术总结
查看>>
Sakai 11.x Build Failure
查看>>
面向对象+模块化设计绘制canvas星空动画
查看>>
Elastic Search学习笔记3——集群配置
查看>>
Unity客户端资源智能管理
查看>>
SVN在Windows下的安装配置步骤
查看>>