{"id":7483,"date":"2025-07-02T15:14:09","date_gmt":"2025-07-02T09:44:09","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=7483"},"modified":"2025-07-22T21:42:35","modified_gmt":"2025-07-22T16:12:35","slug":"isomorphic-strings","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/isomorphic-strings\/","title":{"rendered":"Isomorphic Strings"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link\n  href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\"\n  rel=\"stylesheet\"\n\/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n  .wp_blog_code-tabs-container {\n    font-family: \"Segoe UI\", sans-serif !important;\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    border: 1px solid #ddd !important;\n    border-radius: 8px !important;\n    overflow: hidden !important;\n    background-color: white !important;\n  }\n\n  .wp_blog_code-tabs-header {\n    background: #f7f7f7 !important;\n    display: flex !important;\n    border-bottom: 1px solid #ddd !important;\n  }\n\n  .wp_blog_code-tab-button {\n    flex: 1 !important;\n    padding: 10px 15px !important;\n    border: none !important;\n    background: transparent !important;\n    cursor: pointer !important;\n    font-weight: bold !important;\n    transition: background 0.2s !important;\n    color: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-button.active {\n    background: white !important;\n    border-bottom: 3px solid #0073aa !important;\n  }\n\n  .wp_blog_code-tab-content {\n    display: none !important;\n    padding: 20px !important;\n    background: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-content > pre {\n    background: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-content.active {\n    display: block !important;\n  }\n\n  .wp_blog_code-tab-content pre {\n    margin: 0 !important;\n    overflow-x: auto !important;\n  }\n\n  .wp_blog_explanation {\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    font-family: \"Segoe UI\", sans-serif !important;\n    line-height: 1.6 !important;\n    background: white !important;\n    color: black !important;\n    padding: 1rem !important;\n    border-radius: 8px !important;\n  }\n\n  .wp_blog_explanation h2 {\n    color: #0073aa !important;\n    font-size: 1.5rem !important;\n    margin-bottom: 0.5rem !important;\n  }\n\n  .wp_blog_explanation code {\n    background: #f1f1f1 !important;\n    padding: 2px 6px !important;\n    border-radius: 4px !important;\n    font-family: monospace !important;\n  }\n\n  .wp_blog_explanation h1,\n  .wp_blog_explanation h2,\n  .wp_blog_explanation h3,\n  .wp_blog_explanation h4,\n  .wp_blog_explanation h5,\n  .wp_blog_explanation h6,\n  .wp_blog_explanation p {\n    margin-top: 10px !important;\n    margin-bottom: 10px !important;\n  }\n<\/style>\n\n\n\n<div class=\"wp_blog_explanation\">\n    <p>\n      Two strings <code>s<\/code> and <code>t<\/code> are isomorphic if the characters in <code>s<\/code> can be replaced to get <code>t<\/code>, maintaining a one-to-one mapping between the characters. No two characters may map to the same character but a character may map to itself.\n    <\/p>\n  \n    <h2>Steps<\/h2>\n    <ul>\n      <li>Initialize two maps: one from <code>s<\/code> to <code>t<\/code> and the other from <code>t<\/code> to <code>s<\/code>.<\/li>\n      <li>Traverse both strings simultaneously.<\/li>\n      <li>If a mapping doesn\u2019t exist in either direction, create it.<\/li>\n      <li>If the mapping exists but doesn\u2019t match the current characters, return <code>false<\/code>.<\/li>\n      <li>If the loop completes without conflicts, return <code>true<\/code>.<\/li>\n    <\/ul>\n  \n    <h2>Dry Run<\/h2>\n    <p><strong>Input:<\/strong> <code>s = \"egg\"<\/code>, <code>t = \"add\"<\/code><\/p>\n    <ol>\n      <li>e \u2192 a \u2192 map e \u2192 a, a \u2192 e<\/li>\n      <li>g \u2192 d \u2192 map g \u2192 d, d \u2192 g<\/li>\n      <li>g \u2192 d \u2192 maps exist and match \u2192 OK<\/li>\n      <li>All checks passed \u2192 return <code>true<\/code><\/li>\n    <\/ol>\n  \n    <h2>Time &#038; Space Complexity<\/h2>\n    <ul>\n      <li><strong>Time Complexity:<\/strong> O(n), where n is the length of the strings<\/li>\n      <li><strong>Space Complexity:<\/strong> O(1), as the mappings are bounded by character set size<\/li>\n    <\/ul>\n  <\/div>\n  \n  <div class=\"wp_blog_code-tabs-container\">\n    <div class=\"wp_blog_code-tabs-header\">\n      <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n    <\/div>\n  \n    <!-- JavaScript -->\n    <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n      <pre><code class=\"language-javascript\">\n  var isIsomorphic = function(s, t) {\n      let mapSToT = {};\n      let mapTToS = {};\n  \n      for (let i = 0; i < s.length; i++) {\n          if (!mapSToT[s[i]] &#038;&#038; !mapTToS[t[i]]) {\n              mapSToT[s[i]] = t[i];\n              mapTToS[t[i]] = s[i];\n          } else if (mapTToS[t[i]] !== s[i] || mapSToT[s[i]] !== t[i]) {\n              return false;\n          }\n      }\n  \n      return true;\n  };\n      <\/code><\/pre>\n    <\/div>\n  \n    <!-- C++ -->\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n      <pre><code class=\"language-cpp\">\n  #include &lt;iostream&gt;\n  #include &lt;unordered_map&gt;\n  using namespace std;\n  \n  bool isIsomorphic(string s, string t) {\n      unordered_map&lt;char, char&gt; mapST, mapTS;\n  \n      for (int i = 0; i < s.length(); i++) {\n          char cs = s[i], ct = t[i];\n  \n          if (mapST.count(cs) == 0 &#038;&#038; mapTS.count(ct) == 0) {\n              mapST[cs] = ct;\n              mapTS[ct] = cs;\n          } else {\n              if (mapST[cs] != ct || mapTS[ct] != cs)\n                  return false;\n          }\n      }\n  \n      return true;\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <!-- C -->\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n      <pre><code class=\"language-c\">\n  #include &lt;stdio.h&gt;\n  #include &lt;string.h&gt;\n  \n  int isIsomorphic(char* s, char* t) {\n      int mapST[256] = {0};\n      int mapTS[256] = {0};\n  \n      for (int i = 0; s[i]; i++) {\n          if (mapST[(unsigned char)s[i]] == 0 && mapTS[(unsigned char)t[i]] == 0) {\n              mapST[(unsigned char)s[i]] = t[i];\n              mapTS[(unsigned char)t[i]] = s[i];\n          } else {\n              if (mapST[(unsigned char)s[i]] != t[i] || mapTS[(unsigned char)t[i]] != s[i])\n                  return 0;\n          }\n      }\n  \n      return 1;\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <!-- Java -->\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n      <pre><code class=\"language-java\">\n  import java.util.HashMap;\n  \n  public class Solution {\n      public boolean isIsomorphic(String s, String t) {\n          HashMap&lt;Character, Character&gt; mapST = new HashMap&lt;&gt;();\n          HashMap&lt;Character, Character&gt; mapTS = new HashMap&lt;&gt;();\n  \n          for (int i = 0; i &lt; s.length(); i++) {\n              char cs = s.charAt(i);\n              char ct = t.charAt(i);\n  \n              if (!mapST.containsKey(cs) && !mapTS.containsKey(ct)) {\n                  mapST.put(cs, ct);\n                  mapTS.put(ct, cs);\n              } else {\n                  if (!ct.equals(mapST.get(cs)) || !cs.equals(mapTS.get(ct)))\n                      return false;\n              }\n          }\n  \n          return true;\n      }\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <!-- Python -->\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n      <pre><code class=\"language-python\">\n  def isIsomorphic(s: str, t: str) -> bool:\n      map_s_t = {}\n      map_t_s = {}\n  \n      for cs, ct in zip(s, t):\n          if cs not in map_s_t and ct not in map_t_s:\n              map_s_t[cs] = ct\n              map_t_s[ct] = cs\n          elif map_s_t.get(cs) != ct or map_t_s.get(ct) != cs:\n              return False\n  \n      return True\n      <\/code><\/pre>\n    <\/div>\n  <\/div>\n  \n\n\n<a href=\"https:\/\/leetcode.com\/problems\/two-sum\/description\/\" target=\"blank\"\n  >Solve this problem.<\/a\n>\n<script>\n  document.addEventListener(\"DOMContentLoaded\", function () {\n    const buttons = document.querySelectorAll(\".wp_blog_code-tab-button\");\n    const contents = document.querySelectorAll(\".wp_blog_code-tab-content\");\n\n    buttons.forEach((button) => {\n      button.addEventListener(\"click\", () => {\n        const lang = button.getAttribute(\"data-lang\");\n\n        buttons.forEach((btn) => btn.classList.remove(\"active\"));\n        button.classList.add(\"active\");\n\n        contents.forEach((content) => {\n          content.classList.toggle(\n            \"active\",\n            content.getAttribute(\"data-lang\") === lang\n          );\n        });\n      });\n    });\n  });\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Two strings s and t are isomorphic if the characters in s can be replaced to get t, maintaining a one-to-one mapping between the characters. No two characters may map to the same character but a character may map to itself. Steps Initialize two maps: one from s to t and the other from t<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[322,176,175,174,172,173],"tags":[],"class_list":["post-7483","post","type-post","status-publish","format-standard","category-algorithms-and-data-structures","category-csharp","category-cplusplus","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=7483"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7483\/revisions"}],"predecessor-version":[{"id":7496,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7483\/revisions\/7496"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=7483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=7483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=7483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}