zoukankan      html  css  js  c++  java
  • [LeetCode]Isomorphic Strings

    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.

    Hash Table,由于No two characters may map to the same character but a character may map to itself.

    判断完s->t和t->s即可。

     1 class Solution {
     2 public:
     3     bool isIsomorphic(string s, string t) {
     4         unordered_map<char,char> showed;
     5         for(int i=0;i<s.length();i++)
     6         {
     7             if(showed.find(s[i])!=showed.end())
     8             {
     9                 if(showed[s[i]]!=t[i])
    10                 {
    11                     return false;
    12                 }
    13             }
    14             else
    15             {
    16                 showed[s[i]]=t[i];
    17             }
    18         }
    19         showed.clear();
    20         for(int i=0;i<s.length();i++)
    21         {
    22             if(showed.find(t[i])!=showed.end())
    23             {
    24                 if(showed[t[i]]!=s[i])
    25                 {
    26                     return false;
    27                 }
    28             }
    29             else
    30             {
    31                 showed[t[i]]=s[i];
    32             }
    33         }
    34         return true;
    35     }
    36 };
  • 相关阅读:
    ant脚本打jar包 自动获取时间以及项目svn版本号
    15分钟学会git基本的操作命令
    java后端模拟表单提交
    优秀js插件收藏
    javascript操作
    javascript常用方法整理--数组篇
    javascript exec方法
    javascript 拷贝
    自执行函数简单应用
    jsonp跨域原理解析
  • 原文地址:https://www.cnblogs.com/Sean-le/p/4742195.html
Copyright © 2011-2022 走看看