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.

    Note:
    You may assume both s and t have the same length.

    Hide Tags
     1 // 08:18
     2 class Solution {
     3 public:
     4     bool isIsomorphic(string s, string t) {
     5         int slen = s.size();
     6         int tlen = t.size();
     7         if (slen != tlen) {
     8             return false;
     9         }
    10         char tbl[256] = {0};
    11         bool used[256] = {0};
    12         for (int i=0; i<slen; i++) {
    13             char ch = s[i];
    14             if (tbl[ch] == 0 && !used[t[i]]) {
    15                 tbl[ch] = t[i];
    16                 used[t[i]] = true;
    17                 continue;
    18             }
    19             if (tbl[ch] != t[i]) {
    20                 return false;
    21             }
    22         }
    23         return true;
    24     }
    25 };
     
  • 相关阅读:
    idea中maven自动导入出现问题
    DDIA
    DDIA
    DDIA
    DDIA
    DDIA
    DDIA
    DDIA
    MIT 6.824 第五次作业Primary-Backup Replication
    MIT 6.824 第四次作业GFS
  • 原文地址:https://www.cnblogs.com/lailailai/p/4483803.html
Copyright © 2011-2022 走看看