【Leetcode】【Easy】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.
Note:
You may assume both s and t have the same length.
解题思路:
如何确定两个字符串是形状相同的,即只依靠替代字母形式,能相互转化;
规律是:字符串s和t中相同位置的字符,所有出现的位置都相同。
如果能记录每个字符出现的位置,同时遍历两个字符串,当遍历到新的字符时,检查各自字符之前出现的位置,之前的位置不一致,则返回false,否则继续检查下一对字符。
对于记录位置和查找操作,最方便的是使用hash字典。字典中记录的是此字符上一次出现的位置。
代码为:
class Solution {
public:
bool isIsomorphic(string s, string t) {
map<char, int> char_s;
map<char, int> char_t;
int len = s.size();
for (int i = ; i < len; ++i) {
if (!char_s.count(s[i]))
char_s[s[i]] = i;
if (!char_t.count(t[i]))
char_t[t[i]] = i;
if (char_s[s[i]] != char_t[t[i]])
return false;
char_s[s[i]] = i;
char_t[t[i]] = i;
}
return true;
}
};
【Leetcode】【Easy】Isomorphic Strings的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode题意分析&解答】43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【LeetCode每天一题】Multiply Strings(字符串乘法)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- 【leetcode刷题笔记】Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)
题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
随机推荐
- ActiveMQ安装及使用
1 安装环境 1.需要jdk2.安装Linux系统.生产环境都是Linux系统. 2 安装步骤 第一步: 把ActiveMQ 的压缩包上传到Linux系统.第二步:解压缩. 第三步:关闭防火墙 临时关 ...
- 通过面试题,让我们来了解Collection
前言 欢迎关注公众号:Coder编程 获取最新原创技术文章和相关免费学习资料,随时随地学习技术知识!** 本章主要介绍Collection集合相关知识,结合面试中会提到的相关问题进行知识点的梳理.希望 ...
- 枚举类型与Switch
1.枚举类型,就是一个集合,集合内所有的元素都是枚举类型的, 主要是应用在可预计的集合中,(你知道它的值就只有那么几种情况,这时就可以使用枚举类型) 如: //结果一般只有两种,成功与失败 publi ...
- strcpy和strncpy
在c语言中,对于简单变量,如int型.double型,直接使用赋值符号“=”,即可完成赋值,如 int a=10: int b: b=a: 即可完成用a给b赋值. 但是对于字符串,这样赋值是不准确的. ...
- Linux下批量修改文件及文件夹所有者及权限
Linux下批量修改文件及文件夹所有者及权限需要使用到两个命令,chmod以及chown 例:对/opt/Oracle/目录下的所有文件与子目录执行相同的权限变更: chmod -R 700 /opt ...
- i.mx android6 输入子系统分析(未完)
参考:http://blog.csdn.net/u010312937/article/details/53285286 https://www.jianshu.com/p/7fca94b330ea ...
- 一些Android的博客,没事翻翻
深入剖析Android系统(推荐) 深入浅出Android系统移植与平台开发系列(推荐) Android系统开篇(小米工程师对系统的整体分析,推荐) Android6.0Framework源码解析系列 ...
- maven+tomcat热部署
1.首先修改tomcat安装目录下的conf文件夹中的tomcat-user.xml文件 <role rolename="manager-gui"/> <role ...
- js格式化文件大小,单位:Bytes、KB、MB、GB
原文出自:https://blog.csdn.net/seesun2012 // 格式化文件大小 function renderSize(value){ if(null==value||value== ...
- linux的环境变量与文件查找
1. 环境变量 1.1 变量 shell 中的变量有不同类型,可参与运算,有作用域限定 变量的作用域即变量的有效范围(比如一个函数中.一个源文件中或者全局范围),在该范围内只能有一个同名变量.一旦离开 ...