Isomorphic Strings
来源:互联网 发布:淘宝店铺怎么设置红包 编辑:程序博客网 时间:2024/06/08 18:08
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.
这道题让我们求同构字符串,就是说原字符串中的每个字符可由另外一个字符替代,可以被其本身替代,相同的字符一定要被同一个字符替代,且一个字符不能被多个字符替代,即不能出现一对多的映射。根据一对一映射的特点,我们需要用两个哈希表分别来记录原字符串和目标字符串中字符出现情况。由于ASCII码只有256个字符,所以我们可以用一个256大小的数组来代替哈希表,并初始化为0,我们遍历原字符串,分别从源字符串和目标字符串取出一个字符,然后分别在两个哈希表中查找其值,若不相等,则返回false,若想等,将其值更新为i + 1。
class Solution {public: bool isIsomorphic(string s, string t) { int m1[256] = {0}, m2[256] = {0}, n = s.size(); for (int i = 0; i < n; ++i) { if (m1[s[i]] != m2[t[i]]) return false; m1[s[i]] = i + 1; m2[t[i]] = i + 1; } return true; }};
阅读全文
0 0
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- 2017,最受欢迎的 15 大 Python 库有哪些?
- 第1章 PHP概述
- ImageLoader源码解析-----ImageLoader的结构
- 谷歌研发能处理多域多任务的机器学习模型——MultiModel
- Redis的配置、启动、操作和关闭
- Isomorphic Strings
- 用sessionStorage设置网页刷新后,仍保留用户点击的radio选项
- Codeforces Boredom
- SSH原理与运用:远程登录
- sqlmap一次实践暴库
- Java用CMD打开指定文件和文件夹
- Hadoop入门你要知道的那些知识
- asm基本命令总结
- 前端开发技能图谱