{"id":9046,"date":"2021-12-05T19:40:43","date_gmt":"2021-12-06T03:40:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9046"},"modified":"2021-12-05T19:42:41","modified_gmt":"2021-12-06T03:42:41","slug":"leetcode-205-isomorphic-strings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-205-isomorphic-strings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 205. Isomorphic Strings"},"content":{"rendered":"\n<p>Given two strings&nbsp;<code>s<\/code>&nbsp;and&nbsp;<code>t<\/code>,&nbsp;<em>determine if they are isomorphic<\/em>.<\/p>\n\n\n\n<p>Two strings&nbsp;<code>s<\/code>&nbsp;and&nbsp;<code>t<\/code>&nbsp;are isomorphic if the characters in&nbsp;<code>s<\/code>&nbsp;can be replaced to get&nbsp;<code>t<\/code>.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"egg\", t = \"add\"\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"foo\", t = \"bar\"\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"paper\", t = \"title\"\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><li><code>t.length == s.length<\/code><\/li><li><code>s<\/code>&nbsp;and&nbsp;<code>t<\/code>&nbsp;consist of any valid ascii character.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counting<\/strong><\/h2>\n\n\n\n<p>The # of distinct pairs e.g. (s[i], t[i]), should be equal to the # of distinct chars in s and t.<br>ex1:<br>set of pairs: {(e, a), (g,d)}<br>set of s: {e, g}<br>set of t: {a, d}<br>For s, we can replace e with a, and replace g with d.<br>ex2:<br>set of pairs: {(f, b), (o, a), (o, r)}<br>set of s: {f, o}<br>set of t: {b, a, r}<br>o can not pair with a, r at the same time.<br>ex3:<br>set of pairs: {(p, t), (a, i), (e, l), (r, e)}<br>set of s: {p, a, e, r}<br>set of t: {t, i, l, e}<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(256*256)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool isIsomorphic(string s, string t) {\n    const int n = s.length();\n    unordered_set<int> s1;    \n    for (int i = 0; i < n; ++i)\n      s1.insert((s[i] << 8) | t[i]);    \n    unordered_set<int> s2(begin(s), end(s));\n    unordered_set<int> s3(begin(t), end(t));\n    return s1.size() == s2.size() && s2.size() == s3.size();\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two strings&nbsp;s&nbsp;and&nbsp;t,&nbsp;determine if they are isomorphic. Two strings&nbsp;s&nbsp;and&nbsp;t&nbsp;are isomorphic if the characters in&nbsp;s&nbsp;can be replaced to get&nbsp;t. All occurrences of a character must be&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177,4],"class_list":["post-9046","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9046"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9046\/revisions"}],"predecessor-version":[{"id":9049,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9046\/revisions\/9049"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}