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

    看两个字符串是否同构,用map就可以解决,不过要注意双向都要检查,代码如下:

     1 class Solution {
     2 public:
     3     bool isIsomorphic(string s, string t) {
     4         map<char, char> m1;
     5         map<char, char> m2;
     6         if(s.size() != t.size())
     7             return false;
     8         for(int i = 0; i < s.size(); ++i){
     9             if(m1.find(s[i]) == m1.end() && m2.find(t[i]) == m2.end()){
    10                 m1[s[i]] = t[i];
    11                 m2[t[i]] = s[i];
    12             }
    13             else if(m1.find(s[i]) != m1.end() && m2.find(t[i]) != m2.end()){
    14                 if(m1[s[i]] != t[i] || m2[t[i]] != s[i])
    15                     return false;
    16             }else{
    17                 return false;
    18             }
    19         }
    20         return true;
    21     }
    22 };
  • 相关阅读:
    .NET-记一次架构优化实战与方案-梳理篇
    .net core实践系列之SSO-跨域实现
    Vue
    C# WPF
    开源框架
    开源框架
    开源框架
    开源框架
    WCF
    WCF
  • 原文地址:https://www.cnblogs.com/-wang-cheng/p/4987700.html
Copyright © 2011-2022 走看看