{"id":8072,"date":"2025-07-20T17:19:08","date_gmt":"2025-07-20T11:49:08","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=8072"},"modified":"2025-07-20T17:21:52","modified_gmt":"2025-07-20T11:51:52","slug":"permutationstring","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/permutationstring\/","title":{"rendered":"Permutation in String"},"content":{"rendered":"\n<!-- Permutation In String -->\n<link\n    href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\"\n    rel=\"stylesheet\"\n\/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.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_theme {\n  --primary: #E58C32;\n  --secondary: #f8c291;\n  --light-bg: #fef9f4;\n  --text-dark: #2d2d2d;\n  --tab-radius: 12px;\n  --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);\n  --code-bg: #001f3f;\n  --code-text: #d4f1ff;\n}\n\n.wp_blog_container {\n  font-family: 'Segoe UI', sans-serif;\n  background: var(--light-bg);\n  margin: 0;\n  padding: 0;\n  color: var(--text-dark);\n}\n\n\/* Heading *\/\n.wp_blog_main-heading {\n  text-align: center;\n  font-size: 2.4rem;\n  color: var(--primary);\n  margin-top: 2.5rem;\n  font-weight: bold;\n}\n\n\/* Explanation Card *\/\n.wp_blog_explanation,\n.wp_blog_code-tabs-container {\n  max-width: 940px;\n  margin: 2rem auto;\n  padding: 2rem;\n  background: white;\n  border-radius: var(--tab-radius);\n  box-shadow: var(--shadow);\n}\n\n\/* Text and Visuals *\/\n.wp_blog_explanation h2{\n  font-size: 1.4rem;\n  color: var(--primary);\n  margin-bottom: 0.5rem;\n}\n.wp_blog_explanation h5{\n  color: var(--primary);\n}\n\n.wp_blog_explanation p,\n.wp_blog_explanation li {\n  font-size: 1.05rem;\n  line-height: 1.7;\n  margin: 0.5rem 0;\n}\n.wp_blog_explanation code {\n  background: #f9cea6;\n  color: #2d2d2d;\n  padding: 3px 6px;\n  border-radius: 4px;\n  font-family: 'Courier New', monospace;\n}\n.wp_blog_explanation img {\n  max-width: 100%;\n  border-radius: var(--tab-radius);\n  margin-top: 1rem;\n  box-shadow: 0 2px 12px rgba(0,0,0,0.06);\n}\n\n\/* Tab Buttons *\/\n.wp_blog_code-tabs-header {\n  display: flex;\n  flex-wrap: wrap;\n  gap: 0.5rem;\n  margin-bottom: 1rem;\n}\n.wp_blog_code-tab-button {\n  padding: 0.6rem 1.2rem;\n  border: 1px solid var(--primary);\n  background: white;\n  color: var(--primary);\n  border-radius: 50px;\n  font-weight: 600;\n  cursor: pointer;\n  transition: all 0.3s ease;\n}\n.wp_blog_code-tab-button:hover {\n  background: var(--secondary);\n}\n.wp_blog_code-tab-button.active {\n  background: var(--primary);\n  color: white;\n}\n\n\/* Code Content *\/\n.wp_blog_code-tab-content {\n  display: none;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n}\n.wp_blog_code-tab-content.active {\n  display: block;\n}\n.wp_blog_code-tab-content pre {\n  margin: 0;\n  padding: 1.5rem;\n  font-size: 1rem;\n  overflow-x: auto;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n  color: var(--code-text);\n}\n<\/style>\n\n<div class=\"wp_blog_container wp_blog_theme\"> \n    <h1 class=\"wp_blog_main-heading\"><\/h1>\n    <div class=\"wp_blog_explanation\">\n        <h2>Problem Statement:<\/h2>\n\n        <p>\n            Given two strings <code>s1<\/code> and <code>s2<\/code>, return <code>true<\/code> if <code>s2<\/code> contains a permutation of <code>s1<\/code>, or <code>false<\/code> otherwise.\n        <\/p>\n\n        <p>\n            In other words, return <code>true<\/code> if one of <code>s1<\/code>&#8216;s permutations is the substring of <code>s2<\/code>.\n        <\/p>\n\n                <h2>Examples:<\/h2>\n                <h5>Example 1:<\/h5>\n                <p><strong>Input:<\/strong> s1 = &#8220;ab&#8221;, s2 = &#8220;eidbaooo&#8221;<\/p>\n                <p><strong>Output:<\/strong> true<\/p>\n                <p>\n                  <strong>Explanation:<\/strong>\n                  s2 contains one permutation of s1 (&#8220;ba&#8221;).\n                <\/p>\n\n                <h5>Example 2:<\/h5>\n                <p><strong>Input:<\/strong> s1 = &#8220;ab&#8221;, s2 = &#8220;eidboaoo&#8221;<\/p>\n                <p><strong>Output:<\/strong> false<\/p>\n                \n                    <h2>Constraints:<\/h2>\n                    <ul>\n                      <li><code>1 <= s1.length, s2.length <= 10<sup>4<\/sup><\/code><\/li>\n                      <li><code>s1<\/code> and <code>s2<\/code> consist of lowercase English letters.<\/li>\n                    <\/ul>\n\n                <h2>Approach<\/h2>\n                <ul>\n                    <li>\n                        Use two frequency arrays <code>hashS<\/code> and <code>hashW<\/code> of size 26 to count character occurrences (assuming lowercase English letters).\n                    <\/li>\n\n                    <li>Fill both arrays with frequencies from <code>s1<\/code> and the first window of <code>s2<\/code> of length equal to <code>s1<\/code>.<\/li>\n\n                    <li>Slide the window across <code>s2<\/code> one character at a time:\n                    <ul>\n                        <li>At each step, compare the two frequency arrays using <code>isHashSame()<\/code>.<\/li>\n                        <li>If they match, it means a permutation of <code>s1<\/code> is present in <code>s2<\/code>, so return <code>true<\/code>.<\/li>\n                        <li>Otherwise, update the window by removing the leftmost character and adding the next character.<\/li>\n                    <\/ul>\n                    <\/li>\n                    <li>If no match is found till the end, return <code>false<\/code>.<\/li>\n                <\/ul>\n\n                <h2>Time Complexity:<\/h2>\n                <li>\n                  <p><strong>Time Complexity = O(n)<\/strong>\n                  <\/li>\n                <h2>Space Complexity:<\/h2>\n                <li>\n                  <p><strong>Space Complexity = O(1)<\/strong><\/p>\n                <\/li>\n\n<h2>Dry Run<\/h2> <div style=\"background: #f9f9f9; border-left: 4px solid var(--primary); padding: 1rem; border-radius: var(--tab-radius); margin: 1rem 0;\"> <p><strong>Input:<\/strong> <code>s1 = \"ab\", s2 = \"abcabcbb\"<\/code><\/p> <pre style=\"white-space: pre-wrap; background: #fff5ea; padding: 1rem; border-radius: 8px; overflow-x: auto;\"> Initialize: \u2192 hashS = Array(26).fill(0) \u2192 hashW = Array(26).fill(0) \u2192 windowLength = 2\nBuild hash for s1 and first window of s2:\ni = 0 \u2192 s1[0] = 'a' \u2192 hashS[0]++\ns2[0] = 'a' \u2192 hashW[0]++\n\u2192 hashS = [1, 0, ..., 0]\n\u2192 hashW = [1, 0, ..., 0]\n\ni = 1 \u2192 s1[1] = 'b' \u2192 hashS[1]++\ns2[1] = 'b' \u2192 hashW[1]++\n\u2192 hashS = [1, 1, 0, ..., 0]\n\u2192 hashW = [1, 1, 0, ..., 0]\n\nSet pointers:\n\u2192 i = 0, j = 1\n\nStart sliding window:\nj = 1 (window = s2[0..1] = \"ab\")\n\u2192 isHashSame(hashS, hashW) = true\n\u2192 Return true\n\n<\/pre> <p><strong>Output:<\/strong> <code>Result: true<\/code><\/p> <\/div>\n        \n        <h2>Visualisation:<\/h2>\n        <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/07\/Screenshot-2025-07-20-at-5.08.02\u202fPM.png\" alt=\"longest\" \/>\n    <\/div>\n    \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=\"py\">Python<\/button>\n            <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/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=\"cs\">C#<\/button>\n        <\/div>\n\n        <!-- JavaScript -->\n        <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n            <pre><code class=\"language-javascript\">\nvar checkInclusion = function(s1, s2) {\n    if (s1.length > s2.length) return false;\n    let hashS = Array(26).fill(0);\n    let hashW = Array(26).fill(0);\n    let windowLength = s1.length;\n    for (let i = 0; i < windowLength; i++) {\n        hashS[s1.charCodeAt(i) - 97]++;\n        hashW[s2.charCodeAt(i) - 97]++;\n    }\n    let i = 0;\n    let j = windowLength - 1;\n\n    while (j < s2.length) {\n        if (isHashSame(hashS, hashW)) {\n            return true;\n        }\n        hashW[s2.charCodeAt(i) - 97]--;\n        i++;\n        j++;\n        if (j < s2.length) {\n            hashW[s2.charCodeAt(j) - 97]++;\n        }\n    }\n    return false;\n};\nvar isHashSame = function(hashS, hashW) {\n    for (let i = 0; i < 26; i++) {\n        if (hashS[i] !== hashW[i]) {\n            return false;\n        }\n    }\n    return true;\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\">\ndef is_hash_same(a, b):\n    return a == b\ndef checkInclusion(s1, s2):\n    if len(s1) > len(s2):\n        return False\n    hashS = [0] * 26\n    hashW = [0] * 26\n    window_length = len(s1)\n    for i in range(window_length):\n        hashS[ord(s1[i]) - ord('a')] += 1\n        hashW[ord(s2[i]) - ord('a')] += 1\n    i = 0\n    j = window_length - 1\n    while j < len(s2):\n        if is_hash_same(hashS, hashW):\n            return True\n        hashW[ord(s2[i]) - ord('a')] -= 1\n        i += 1\n        j += 1\n        if j < len(s2):\n            hashW[ord(s2[j]) - ord('a')] += 1\n    return False\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\">\npublic class Solution {\n    public boolean checkInclusion(String s1, String s2) {\n        if (s1.length() > s2.length()) return false;\n        int[] hashS = new int[26];\n        int[] hashW = new int[26];\n        int windowLength = s1.length();\n\n        for (int i = 0; i < windowLength; i++) {\n            hashS[s1.charAt(i) - 'a']++;\n            hashW[s2.charAt(i) - 'a']++;\n        }\n        int i = 0, j = windowLength - 1;\n        while (j < s2.length()) {\n            if (isHashSame(hashS, hashW)) return true;\n            hashW[s2.charAt(i) - 'a']--;\n            i++;\n            j++;\n            if (j < s2.length())\n                hashW[s2.charAt(j) - 'a']++;\n        }\n        return false;\n    }\n    private boolean isHashSame(int[] a, int[] b) {\n        for (int i = 0; i < 26; i++) {\n            if (a[i] != b[i]) return false;\n        }\n        return true;\n    }\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;vector&gt;\n#include &lt;string&gt;\nusing namespace std;\nbool isHashSame(vector<int>& a, vector<int>& b) {\n    for(int i = 0; i < 26; i++) {\n        if(a[i] != b[i]) return false;\n    }\n    return true;\n}\nbool checkInclusion(string s1, string s2) {\n    if(s1.length() > s2.length()) return false;\n    vector<int> hashS(26, 0), hashW(26, 0);\n    int window_length = s1.length();\n    for(int i = 0; i < window_length; i++) {\n        hashS[s1[i] - 'a']++;\n        hashW[s2[i] - 'a']++;\n    }\n    int i = 0, j = window_length - 1;\n    while(j < s2.length()) {\n        if(isHashSame(hashS, hashW)) return true;\n        hashW[s2[i] - 'a']--;\n        i++;\n        j++;\n        if(j < s2.length())\n            hashW[s2[j] - 'a']++;\n    }\n    return false;\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;stdbool.h&gt;\n#include &lt;string.h&gt;\nbool isHashSame(int *a, int *b) {\n    for(int i = 0; i < 26; i++) {\n        if(a[i] != b[i]) return false;\n    }\n    return true;\n}\nbool checkInclusion(char *s1, char *s2) {\n    int len1 = strlen(s1), len2 = strlen(s2);\n    if(len1 > len2) return false;\n    int hashS[26] = {0}, hashW[26] = {0};\n    for(int i = 0; i < len1; i++) {\n        hashS[s1[i] - 'a']++;\n        hashW[s2[i] - 'a']++;\n    }\n    int i = 0, j = len1 - 1;\n    while(j < len2) {\n        if(isHashSame(hashS, hashW)) return true;\n        hashW[s2[i] - 'a']--;\n        i++;\n        j++;\n        if(j < len2)\n            hashW[s2[j] - 'a']++;\n    }\n    return false;\n}\n            <\/code><\/pre>\n        <\/div>\n\n        <!-- C# -->\n        <div class=\"wp_blog_code-tab-content\" data-lang=\"cs\">\n            <pre><code class=\"language-csharp\">\nusing System;\nclass Solution {\n    public bool CheckInclusion(string s1, string s2) {\n        if (s1.Length > s2.Length) return false;\n        int[] hashS = new int[26];\n        int[] hashW = new int[26];\n        int window_length = s1.Length;\n        for (int i = 0; i < window_length; i++) {\n            hashS[s1[i] - 'a']++;\n            hashW[s2[i] - 'a']++;\n        }\n        int i = 0, j = window_length - 1;\n        while (j < s2.Length) {\n            if (IsHashSame(hashS, hashW)) return true;\n            hashW[s2[i] - 'a']--;\n            i++;\n            j++;\n            if (j < s2.Length)\n                hashW[s2[j] - 'a']++;\n        }\n        return false;\n    }\n    private bool IsHashSame(int[] a, int[] b) {\n        for (int i = 0; i < 26; i++) {\n            if (a[i] != b[i]) return false;\n        }\n        return true;\n    }\n}\n       <\/code><\/pre>\n        <\/div>\n    <\/div>\n<\/div>\n\n<script>\n    document.addEventListener(\"DOMContentLoaded\", () => {\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                contents.forEach((content) =>\n                    content.classList.remove(\"active\")\n                );\n\n                button.classList.add(\"active\");\n                document\n                    .querySelector(`.wp_blog_code-tab-content[data-lang=\"${lang}\"]`)\n                    .classList.add(\"active\");\n            });\n        });\n    });\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement: Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. In other words, return true if one of s1&#8216;s permutations is the substring of s2. Examples: Example 1: Input: s1 = &#8220;ab&#8221;, s2 = &#8220;eidbaooo&#8221; Output: true Explanation: s2 contains one permutation of s1 (&#8220;ba&#8221;).<\/p>\n","protected":false},"author":108,"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":[210,322,260,176,175,211,811,810,174,172,173],"tags":[],"class_list":{"0":"post-8072","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-algorithms","7":"category-algorithms-and-data-structures","8":"category-c-c-plus-plus","9":"category-csharp","10":"category-cplusplus","11":"category-data-structures","12":"category-data-structures-and-algorithms","13":"category-dsa","14":"category-java","15":"category-javascript","16":"category-python"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8072","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\/108"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=8072"}],"version-history":[{"count":3,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8072\/revisions"}],"predecessor-version":[{"id":8076,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8072\/revisions\/8076"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=8072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=8072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=8072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}