{"id":7476,"date":"2025-07-02T15:00:58","date_gmt":"2025-07-02T09:30:58","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=7476"},"modified":"2026-05-05T09:00:56","modified_gmt":"2026-05-05T03:30:56","slug":"split-a-string-in-balanced-strings","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/split-a-string-in-balanced-strings\/","title":{"rendered":"Split a string in balanced 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<div class=\"wp_blog_explanation\">    \n  <p>\n    This problem focuses on splitting a string into the maximum number of balanced substrings.\n    A string is considered balanced if it contains an equal number of &#8216;L&#8217; and &#8216;R&#8217; characters.\n    The goal is to iterate through the string and count how many such balanced splits can be formed.\n  <\/p>\n\n  <h2>Steps<\/h2>\n  <ul>\n    <li>Initialize a counter variable to track balance.<\/li>\n    <li>Traverse the string character by character.<\/li>\n    <li>If the character is &#8216;R&#8217;, increment the counter.<\/li>\n    <li>If the character is &#8216;L&#8217;, decrement the counter.<\/li>\n    <li>Whenever the counter becomes zero, it means a balanced substring is found.<\/li>\n    <li>Increment the result count.<\/li>\n    <li>Return the total count.<\/li>\n  <\/ul>\n\n  <h2>Dry Run<\/h2>\n  <p><strong>Input:<\/strong> <code>s = \"RLRRLLRLRL\"<\/code><\/p>\n  <ol>\n    <li>R \u2192 temp = 1<\/li>\n    <li>L \u2192 temp = 0 \u2192 count = 1<\/li>\n    <li>R \u2192 temp = 1<\/li>\n    <li>R \u2192 temp = 2<\/li>\n    <li>L \u2192 temp = 1<\/li>\n    <li>L \u2192 temp = 0 \u2192 count = 2<\/li>\n    <li>R \u2192 temp = 1<\/li>\n    <li>L \u2192 temp = 0 \u2192 count = 3<\/li>\n    <li>R \u2192 temp = 1<\/li>\n    <li>L \u2192 temp = 0 \u2192 count = 4<\/li>\n    <li>Final Answer = <code>4<\/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 string<\/li>\n    <li><strong>Space Complexity:<\/strong> O(1), uses constant space<\/li>\n  <\/ul>\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=\"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\">\nvar balancedStringSplit = function(s) {\n    let temp = 0;\n    let count = 0;\n    for(let i = 0; i < s.length; i++) {\n        if(s[i] == 'R') {\n            ++temp;\n        } else {\n            --temp;\n        }\n        if(temp == 0) {\n            ++count;\n        }\n    }\n    return count;\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;string>\nusing namespace std;\n\nclass Solution {\npublic:\n    int balancedStringSplit(string s) {\n        int temp = 0, count = 0;\n        for(char c : s) {\n            if(c == 'R') temp++;\n            else temp--;\n            \n            if(temp == 0) count++;\n        }\n        return count;\n    }\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\">\nint balancedStringSplit(char* s) {\n    int temp = 0, count = 0;\n    for(int i = 0; s[i] != '\\0'; i++) {\n        if(s[i] == 'R') temp++;\n        else temp--;\n        \n        if(temp == 0) count++;\n    }\n    return count;\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\">\nclass Solution {\n    public int balancedStringSplit(String s) {\n        int temp = 0, count = 0;\n        for(char c : s.toCharArray()) {\n            if(c == 'R') temp++;\n            else temp--;\n            \n            if(temp == 0) count++;\n        }\n        return count;\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\">\nclass Solution(object):\n    def balancedStringSplit(self, s):\n        temp = 0\n        count = 0\n        for ch in s:\n            if ch == 'R':\n                temp += 1\n            else:\n                temp -= 1\n            \n            if temp == 0:\n                count += 1\n        return count\n  <\/code><\/pre>\n<\/div>\n  <\/div>\n  \n  \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\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This problem focuses on splitting a string into the maximum number of balanced substrings. A string is considered balanced if it contains an equal number of &#8216;L&#8217; and &#8216;R&#8217; characters. The goal is to iterate through the string and count how many such balanced splits can be formed. Steps Initialize a counter variable to track<\/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":[1],"tags":[],"class_list":["post-7476","post","type-post","status-publish","format-standard","category-uncategorized"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7476","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=7476"}],"version-history":[{"count":4,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7476\/revisions"}],"predecessor-version":[{"id":12234,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7476\/revisions\/12234"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=7476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=7476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=7476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}