leetcode@ [205] Isomorphic Strings
2015-12-02 16:13
211 查看
https://leetcode.com/problems/isomorphic-strings/
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
Given
Given
leetcode 205: Isomorphic Strings
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.
class Solution { public: bool isIsomorphic(string s, string t) { if(s.empty() && t.empty()) return true; if(s.length() != t.length()) return false; map<char, char> s_t_hsh; s_t_hsh.clear(); map<char, char> t_s_hsh; t_s_hsh.clear(); map<char, char>::iterator p_s_t, p_t_s; for(int i=0;i<s.length();++i) { p_s_t = s_t_hsh.find(s[i]); p_t_s = t_s_hsh.find(t[i]); if(p_s_t != s_t_hsh.end() && p_s_t->second != t[i]) return false; else s_t_hsh.insert(pair<char,char> (s[i],t[i])); if(p_t_s != t_s_hsh.end() && p_t_s->second != s[i]) return false; else t_s_hsh.insert(pair<char,char> (t[i],s[i])); } return true; } };
leetcode 205: Isomorphic Strings
相关文章推荐
- IDirect3DDevice9::SetTexture的stage参数
- PowerDesigner(七)-数据库的生成和修改
- CentOS7下编译openjdk
- Oracle存储过程详解
- (六)redis 006-key操作
- javascript实现获取浏览器版本、浏览器类型
- Spring MVC 3.1新特性 生产者、消费者请求限定
- [leetcode] 135. Candy
- PowerDesigner(六)-物理数据模型(PDM逆向工程)
- vim 笔记
- 用RSA加密实现Web登录密码加密传输
- 几种封装javaBean的方法
- C-027.c语言中字符串与字符数组
- 面试心得
- nginx搭建流媒体服务器
- nginx搭建流媒体服务器
- (五)redis sortedset讲解
- PowerDesigner(五)-概念数据模型(CDM生成LDM,PDM和OOM)
- 为什么不能用memcached存储Session
- 回溯法解决N皇后问题