{"id":7477,"date":"2025-07-02T15:03:46","date_gmt":"2025-07-02T09:33:46","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=7477"},"modified":"2025-07-02T15:03:46","modified_gmt":"2025-07-02T09:33:46","slug":"reverse-string-ii","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/reverse-string-ii\/","title":{"rendered":"Reverse String II"},"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\">    <p>\n      The problem requires reversing the first <code>k<\/code> characters for every 2<code>k<\/code> characters in a string. If there are fewer than <code>k<\/code> characters left, reverse all of them. If there are between <code>k<\/code> and <code>2k<\/code> characters left, reverse the first <code>k<\/code> and leave the rest as is.\n    <\/p>\n  \n    <h2>Steps<\/h2>\n    <ul>\n      <li>Convert the string into a character array (if needed).<\/li>\n      <li>Iterate over the array in steps of <code>2k<\/code>.<\/li>\n      <li>At each step, reverse the next <code>k<\/code> characters if available.<\/li>\n      <li>Join or return the modified string.<\/li>\n    <\/ul>\n  \n    <h2>Dry Run<\/h2>\n    <p><strong>Input:<\/strong> <code>s = \"abcdefg\", k = 2<\/code><\/p>\n    <ol>\n      <li>First 2k = 4 chars: reverse first 2 \u2192 <code>\"bacd\"<\/code><\/li>\n      <li>Remaining = &#8220;efg&#8221;: reverse first 2 \u2192 <code>\"fe\"<\/code>, keep &#8220;g&#8221;<\/li>\n      <li><strong>Output:<\/strong> <code>\"bacdfeg\"<\/code><\/li>\n    <\/ol>\n  \n    <h2>Time &#038; Space Complexity<\/h2>\n    <ul>\n      <li><strong>Time Complexity:<\/strong> O(n), where n = length of the string<\/li>\n      <li><strong>Space Complexity:<\/strong> O(1) if in-place, else O(n)<\/li>\n    <\/ul>\n  <\/div>\n  \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\">\n  var reverseStr = function (s, k) {\n      s = s.split(\"\")\n  \n      for (let x = 0; x < s.length; x = x + (2 * k)) {\n          let n = Math.min(k, s.length - x)\n          let mid = Math.floor(n \/ 2)\n          for (let i = 0; i < mid; i++) {\n              let temp = s[x + i]\n              s[x + i] = s[x + n - 1 - i]\n              s[x + n - 1 - i] = temp\n          }\n      }\n      return s.join(\"\")\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;string&gt;\n  #include &lt;algorithm&gt;\n  using namespace std;\n  \n  string reverseStr(string s, int k) {\n      for (int i = 0; i < s.length(); i += 2 * k) {\n          int end = min(i + k, (int)s.length());\n          reverse(s.begin() + i, s.begin() + end);\n      }\n      return s;\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  void reverse(char* s, int start, int end) {\n      while (start < end) {\n          char temp = s[start];\n          s[start] = s[end];\n          s[end] = temp;\n          start++;\n          end--;\n      }\n  }\n  \n  void reverseStr(char* s, int k) {\n      int len = strlen(s);\n      for (int i = 0; i < len; i += 2 * k) {\n          int end = (i + k < len) ? i + k - 1 : len - 1;\n          reverse(s, i, end);\n      }\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  public class Solution {\n      public String reverseStr(String s, int k) {\n          char[] arr = s.toCharArray();\n          for (int i = 0; i &lt; arr.length; i += 2 * k) {\n              int left = i;\n              int right = Math.min(i + k - 1, arr.length - 1);\n              while (left &lt; right) {\n                  char temp = arr[left];\n                  arr[left] = arr[right];\n                  arr[right] = temp;\n                  left++;\n                  right--;\n              }\n          }\n          return new String(arr);\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 reverseStr(s: str, k: int) -> str:\n      s = list(s)\n      for i in range(0, len(s), 2 * k):\n          s[i:i + k] = reversed(s[i:i + k])\n      return ''.join(s)\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>The problem requires reversing the first k characters for every 2k characters in a string. If there are fewer than k characters left, reverse all of them. If there are between k and 2k characters left, reverse the first k and leave the rest as is. Steps Convert the string into a character array (if<\/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-7477","post","type-post","status-publish","format-standard","category-uncategorized"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7477","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=7477"}],"version-history":[{"count":2,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7477\/revisions"}],"predecessor-version":[{"id":8191,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7477\/revisions\/8191"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=7477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=7477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=7477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}