zoukankan      html  css  js  c++  java
  • LeetCode#205 Isomorphic Strings

    Problem Definition:

    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.

    Solution: 利用字典 dict, 一次遍历,双向映射

     1 def isIsomorphic(s, t):
     2         d1,d2={},{}
     3         for a,b in zip(s,t):
     4             if a not in d1:
     5                 d1[a]=b
     6             else:
     7                 if d1[a]!=b:
     8                     return False
     9             if b not in d2:
    10                 d2[b]=a
    11             else:
    12                 if d2[b]!=a:
    13                     return False
    14         return True
  • 相关阅读:
    01矩阵扩展
    蒙特卡罗仿真
    某幂相关数学结论
    分式乘法变加减
    ICPC模板排版工具
    windows下mysql使用实录
    随机题目小结
    工作用linux命令汇总
    小数化分数的O(log2n)解法
    博弈总结
  • 原文地址:https://www.cnblogs.com/acetseng/p/4654205.html
Copyright © 2011-2022 走看看