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实现。

代码如下:

  1. class Solution {
  2. public:
  3. bool isIsomorphic(string s, string t) {
  4. map<char, char> mapA;
  5. map<char, char> mapB;
  6. int n = s.length();
  7. for(int i = ; i < n; i++)
  8. {
  9. char ss = s[i];
  10. char tt = t[i];
  11. map<char, char>::iterator it = mapA.find(ss);
  12. if(it != mapA.end())
  13. {
  14. if(mapA[ss] != tt)
  15. {
  16. return false;
  17. }
  18. }
  19. else
  20. {
  21. map<char, char>::iterator ii = mapB.find(tt);
  22. if(ii != mapB.end() && mapB[tt] != ss)
  23. {
  24. return false;
  25. }
  26. else
  27. {
  28. mapA[ss] = tt;
  29. mapB[tt] = ss;
  30. }
  31. }
  32. }
  33. return true;
  34. }
  35. };

leetcode 205的更多相关文章

  1. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

  2. LeetCode 205. Isomorphic Strings (同构字符串)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  3. LeetCode 205. 同构字符串(Isomorphic Strings)

    205. 同构字符串 205. Isomorphic Strings

  4. Java实现 LeetCode 205 同构字符串

    205. 同构字符串 给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序. ...

  5. Java for LeetCode 205 Isomorphic Strings

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  6. Leetcode 205 Isomorphic Strings 字符串处理

    判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...

  7. (easy)LeetCode 205.Reverse Linked List

    Reverse a singly linked list. 解法一:记录单链表每个节点的val,然后重新为单链表赋值.(取巧,仅仅是将val部分改变,原始node节点并没有改变) 代码如下: /** ...

  8. (easy)LeetCode 205.Isomorphic Strings (*)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  9. Java [Leetcode 205]Isomorphic Strings

    题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

随机推荐

  1. IOS上解决内存越界访问问题

    IOS经常会混合使用C代码,而在C中,对内存的读写是很频繁的操作. 其中,内存越界读写 unsigned char* p =(unsigned char*)malloc(10); unsigned c ...

  2. Thread基本介绍

    1.Thread类介绍 Class Thread java.lang.Object java.lang.Thread All Implemented Interfaces: Runnable Dire ...

  3. 服务器Ubuntu16.04下连接锐捷

    最近搞深度学习,老师买了一台服务器.双系统,win7和Ubuntu16.04,但是联网是遇到了问题. 输入ifconfig时,发现根本就找不到eth0和eth1,只有evp0s25和evp0s90,o ...

  4. 富文本编辑器防止xss注入javascript版

    富文本编辑器:ueditor 其实富文本编辑器已经有防止xss注入功能,但是你服务端程序在接收的时候在做一次转义,否则有可能然后前端验证直接提交数据导致被xss攻击. 为了节省后端程序开销则在前端 显 ...

  5. fedora wine qq

    http://blog.lilydjwg.me/2015/10/26/run-tencent-qq-lite-with-wine.186640.html

  6. 《大型网站系统与Java中间件实践》读书笔记

    分布式系统的基础知识 阿姆达尔定律 多线程交互模式 互不通信,没有交集,各自执行各自的任务和逻辑 基于共享容器(如队列)协同的多线程模式->生产者-消费者->队列 通过事件协同的多线程模式 ...

  7. Eclipse FreeMarker 插件安装

    方法一:手动安装 手动安装没有成功 步骤: 1. 下载freemarker-ide : http://sourceforge.net/projects/freemarker-ide/files/ 2. ...

  8. 【学】AngularJS日记(2)

    数组循环放到新生成的li中 <ul ng-init="arr=[12,5,6,394,344]"> <li ng-repeat="item in arr ...

  9. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  10. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...