LeetCode: Isomorphic Strings
2015-06-03 12:38
302 查看
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
return true.
Given
return false.
Given
return true.
Note:
You may assume both s and t have the same length.
class Solution {
public:
bool isIsomorphic(string s, string t) {
if(s.size() != t.size())
return false;
unordered_map<char, char> m_map;
unordered_map<char, bool> dulplicate;
for(int i = 0; i < s.size(); i++)
{
if(m_map.find(s[i]) == m_map.end())
{
if(dulplicate[t[i]] == true)
return false;
m_map[s[i]] = t[i];
dulplicate[t[i]] = true;
}
else
{
if(m_map[s[i]] != t[i])
return false;
}
}
return true;
}
};
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.
class Solution {
public:
bool isIsomorphic(string s, string t) {
if(s.size() != t.size())
return false;
unordered_map<char, char> m_map;
unordered_map<char, bool> dulplicate;
for(int i = 0; i < s.size(); i++)
{
if(m_map.find(s[i]) == m_map.end())
{
if(dulplicate[t[i]] == true)
return false;
m_map[s[i]] = t[i];
dulplicate[t[i]] = true;
}
else
{
if(m_map[s[i]] != t[i])
return false;
}
}
return true;
}
};
相关文章推荐
- 使用RR格式
- 三、数组初始化
- Azure VM上传小文件
- <li>元素的排序
- WdatePicker()时间控制方式(转载+原创)
- WdatePicker()时间控制方式(转载+原创)
- UITextField和UITextView详解
- Android事件处理方法总结-基于回调的事件处理
- 共享经济与优步(Uber)如何获取监管支持?
- sysdate用法大全
- Python基于DES算法加密解密实例
- HDU1215---七夕节(小思维题)
- 【AAA云】免费云主机,推荐码,有效期:2015年8月2日
- 文章标题
- CodeIgniter_2 路由中定义伪静态 直接映射到相关的控制器
- ON DUPLICATE KEY UPDATE
- Ewebeditor最新漏洞及漏洞大全
- 1002: [FJOI2007]轮状病毒
- C#开发微信门户及应用-使用地理位置扩展相关应用
- 疯狂Java学习笔记(70)-----------挚爱Java