zoukankan      html  css  js  c++  java
  • [LeetCode]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.

    解题思路:

    用哈希表来存储字符出现的位置,然后比较同型字符出现的位置,如果不等返回false。

     1 class Solution {
     2 public:
     3     bool isIsomorphic(string s, string t) {
     4         int len = s.size();
     5         
     6         unordered_map<int, int> cache1;
     7         unordered_map<int, int> cache2;
     8         for (int i = 0; i < len; ++i) {
     9             auto c1 = cache1.find(s[i]);
    10             auto c2 = cache2.find(t[i]);
    11             
    12             if (c1 == cache1.end() && c2 == cache2.end()) {
    13                 cache1[s[i]] = i;
    14                 cache2[t[i]] = i;
    15             } else if ((c1 == cache1.end() && c2 != cache2.end()) || 
    16                       (c1 != cache1.end() && c2 == cache2.end()) || 
    17                       (c1->second != c2->second)) {
    18                 return false;
    19             }
    20         }
    21         
    22         return true;
    23     }
    24 };
  • 相关阅读:
    007_排序_多重排序
    Locust 运行模式
    Locust介绍
    Locust环境搭建及报错解决
    8-02全局变量与输出语句
    8-01变量
    7-15ALL、 ANY、SOME子查询
    7-14 EXISTS子查询
    7-13IN和NOT IN 子查询
    7-12简单子查询
  • 原文地址:https://www.cnblogs.com/skycore/p/4965243.html
Copyright © 2011-2022 走看看