leetcode 205
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.
Note:
You may assume both s and t have the same length.
设定两个字符串长度相等,所谓的“同构”,就是字符串 s 中的字符可以一对一的映射到字符串 t 中的字符。不能一对多,也不能多对一。
利用两个map实现。
代码如下:
- class Solution {
- public:
- bool isIsomorphic(string s, string t) {
- map<char, char> mapA;
- map<char, char> mapB;
- int n = s.length();
- for(int i = ; i < n; i++)
- {
- char ss = s[i];
- char tt = t[i];
- map<char, char>::iterator it = mapA.find(ss);
- if(it != mapA.end())
- {
- if(mapA[ss] != tt)
- {
- return false;
- }
- }
- else
- {
- map<char, char>::iterator ii = mapB.find(tt);
- if(ii != mapB.end() && mapB[tt] != ss)
- {
- return false;
- }
- else
- {
- mapA[ss] = tt;
- mapB[tt] = ss;
- }
- }
- }
- return true;
- }
- };
leetcode 205的更多相关文章
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- LeetCode 205. Isomorphic Strings (同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- Java实现 LeetCode 205 同构字符串
205. 同构字符串 给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序. ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Leetcode 205 Isomorphic Strings 字符串处理
判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...
- (easy)LeetCode 205.Reverse Linked List
Reverse a singly linked list. 解法一:记录单链表每个节点的val,然后重新为单链表赋值.(取巧,仅仅是将val部分改变,原始node节点并没有改变) 代码如下: /** ...
- (easy)LeetCode 205.Isomorphic Strings (*)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Java [Leetcode 205]Isomorphic Strings
题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
随机推荐
- IOS上解决内存越界访问问题
IOS经常会混合使用C代码,而在C中,对内存的读写是很频繁的操作. 其中,内存越界读写 unsigned char* p =(unsigned char*)malloc(10); unsigned c ...
- Thread基本介绍
1.Thread类介绍 Class Thread java.lang.Object java.lang.Thread All Implemented Interfaces: Runnable Dire ...
- 服务器Ubuntu16.04下连接锐捷
最近搞深度学习,老师买了一台服务器.双系统,win7和Ubuntu16.04,但是联网是遇到了问题. 输入ifconfig时,发现根本就找不到eth0和eth1,只有evp0s25和evp0s90,o ...
- 富文本编辑器防止xss注入javascript版
富文本编辑器:ueditor 其实富文本编辑器已经有防止xss注入功能,但是你服务端程序在接收的时候在做一次转义,否则有可能然后前端验证直接提交数据导致被xss攻击. 为了节省后端程序开销则在前端 显 ...
- fedora wine qq
http://blog.lilydjwg.me/2015/10/26/run-tencent-qq-lite-with-wine.186640.html
- 《大型网站系统与Java中间件实践》读书笔记
分布式系统的基础知识 阿姆达尔定律 多线程交互模式 互不通信,没有交集,各自执行各自的任务和逻辑 基于共享容器(如队列)协同的多线程模式->生产者-消费者->队列 通过事件协同的多线程模式 ...
- Eclipse FreeMarker 插件安装
方法一:手动安装 手动安装没有成功 步骤: 1. 下载freemarker-ide : http://sourceforge.net/projects/freemarker-ide/files/ 2. ...
- 【学】AngularJS日记(2)
数组循环放到新生成的li中 <ul ng-init="arr=[12,5,6,394,344]"> <li ng-repeat="item in arr ...
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...