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.

秒小题好爽的说。

思路:保存两个字符串字母对应的字母,出现不一致就false

我的代码:

  1. bool isIsomorphic(string s, string t) {
  2. unordered_map<char, char> map1, map2;
  3. for(int i = ; i < s.size(); ++i)
  4. {
  5. if(map1.find(s[i]) == map1.end() && map2.find(t[i]) == map2.end())
  6. {
  7. map1[s[i]] = t[i]; map2[t[i]] = s[i];
  8. }
  9. else if(map1[s[i]] != t[i] || map2[t[i]] != s[i])
  10. return false;
  11. else;
  12. }
  13. return true;
  14. }

网上C版本代码 免去了查找 更快

  1. bool isIsomorphic(char* s, char* t) {
  2. char mapST[] = { };
  3. char mapTS[] = { };
  4. size_t len = strlen(s);
  5. for (int i = ; i < len; ++i)
  6. {
  7. if (mapST[s[i]] == && mapTS[t[i]] == )
  8. {
  9. mapST[s[i]] = t[i];
  10. mapTS[t[i]] = s[i];
  11. }
  12. else
  13. {
  14. if (mapST[s[i]] != t[i] || mapTS[t[i]] != s[i])
  15. return false;
  16. }
  17. }
  18. return true;
  19. }

【leetcode】Isomorphic Strings(easy)的更多相关文章

  1. 【leetcode】Isomorphic Strings

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

  2. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. 【leetcode】Same Tree(easy)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  5. 【leetcode】Remove Element (easy)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  6. 【leetcode】Min Stack(easy)

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  7. 【leetcode】Count Primes(easy)

    Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...

  8. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  9. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

随机推荐

  1. Web渗透测试使用Kali Linux(一)渗透测试概要及环境部署

    渗透测试是利用已经发现的漏洞,采用恶意黑客的惯用手段来尝试对漏洞进行攻击. Kali Linux是BackTrack的进化版,是Linux的衍生版本,专门开发用作渗透测试,其中提供了很多的渗透测试工具 ...

  2. select2

    .select2-container .select2-choice { height: 34px; line-height: 34px; } .自定义 组件高度 在css 里面设置 .select2 ...

  3. eclipse无法自动识别出svn项目

    因为重新安装了svn插件,重启后发现原来的svn项目无法自动识别出来,连Team->Share Project都没有,而本地用tortoiseSvn是可以正常操作的. 后来我把项目删除然后重新导 ...

  4. hdu.1254.推箱子(bfs + 优先队列)

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  5. C#调用java类、jar包方法

    一.将已经编译后的java中Class文件进行打包:打包命令JAR 如:将某目录下的所有class文件夹全部进行打包处理: 使用的命令:jar cvf test.jar -C com/ . 其中tes ...

  6. DOM之节点层次

    1.1 Node类型 DOM1级定义了一个Node接口,该接口将由DOM中的所有节点类型实现.这个Node接口在JS中是作为Node类型实现的:除了IE之外,其他浏览器可访问这个类型.JS中的所有节点 ...

  7. 通过NavMeshObstacle解决NavMesh防卡

    http://www.unity蛮牛.com/thread-33383-1-1.html. 许久未曾发帖了,最近忙于换工作的问题,经常处于纠结状态,so...偶尔上蛮牛还能看到大家对我的支持,感觉还是 ...

  8. web.xml 模板和Servlet版本

    最近没事干,写自己小项目(项目周期无限长.开发效率无限低)的时候,遇到web.xml的dtd声明不正确,这里罗列下从Eclipse里新建项目时,自动生成的web.xml,供以后遇到类似问题的时候进行参 ...

  9. OpenCV人形检测Hog

    #include "iostream" #include "queue" using namespace std; #include "opencv2 ...

  10. 33 网络相关函数(一)——live555源码阅读(四)网络

    33 网络相关函数(一)——live555源码阅读(四)网络 33 网络相关函数(一)——live555源码阅读(四)网络 简介 1)IsMulticastAddress多播(组播)地址判断函数 多播 ...