{"id":6154,"date":"2025-05-29T18:16:20","date_gmt":"2025-05-29T12:46:20","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6154"},"modified":"2025-06-05T18:37:15","modified_gmt":"2025-06-05T13:07:15","slug":"reverse-string","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/reverse-string\/","title":{"rendered":"Reverse string"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\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<!-- \u2705 START: Blog Explanation -->\n<div class=\"wp_blog_explanation\">\n  <p>\n    Write a function that reverses a string. The input string is given as an array of characters <code>s<\/code>. You must do this by modifying the input array in-place with O(1) extra memory.\n  <\/p>\n\n  <h2>Examples:<\/h2>\n  <p><strong>Example 1:<\/strong><\/p>\n  <pre><code>\nInput: s = [\"h\",\"e\",\"l\",\"l\",\"o\"]\nOutput: [\"o\",\"l\",\"l\",\"e\",\"h\"]\n  <\/code><\/pre>\n\n  <p><strong>Example 2:<\/strong><\/p>\n  <pre><code>\nInput: s = [\"H\",\"a\",\"n\",\"n\",\"a\",\"h\"]\nOutput: [\"h\",\"a\",\"n\",\"n\",\"a\",\"H\"]\n  <\/code><\/pre>\n\n  <h2>Approach: Two Pointer Technique<\/h2>\n  <ol>\n    <li>Initialize two pointers, one at the start and one at the end of the array.<\/li>\n    <li>Swap the characters at both pointers.<\/li>\n    <li>Move the pointers towards the center until they meet.<\/li>\n  <\/ol>\n\n  <h2>Dry Run<\/h2>\n  <pre><code>\nInput: s = [\"h\", \"e\", \"l\", \"l\", \"o\"]\nlen = 5, halfLen = 2\n\ni = 0 \u2192 swap s[0] and s[4] \u2192 [\"o\", \"e\", \"l\", \"l\", \"h\"]\ni = 1 \u2192 swap s[1] and s[3] \u2192 [\"o\", \"l\", \"l\", \"e\", \"h\"]\n\nResult: [\"o\", \"l\", \"l\", \"e\", \"h\"]\n  <\/code><\/pre>\n\n  <h2>Complexity<\/h2>\n  <ul>\n    <li><strong>Time:<\/strong> O(N)<\/li>\n    <li><strong>Space:<\/strong> O(1)<\/li>\n  <\/ul>\n<\/div>\n<!-- \u2705 END: Blog Explanation -->\n\n<!-- \u2705 START: Code Tabs -->\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    <button class=\"wp_blog_code-tab-button\" data-lang=\"csharp\">C#<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">\nvar reverseString = function(s) {\n    let len = s.length;\n    let halfLen = Math.floor(len \/ 2);\n\n    for (let i = 0; i < halfLen; i++) {\n        let temp = s[i];\n        s[i] = s[len - i - 1];\n        s[len - i - 1] = temp;\n    }\n};\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">\nclass Solution {\npublic:\n    void reverseString(vector&lt;char&gt;& s) {\n        int left = 0, right = s.size() - 1;\n        while (left < right) {\n            swap(s[left], s[right]);\n            left++;\n            right--;\n        }\n    }\n};\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">\nvoid reverseString(char* s, int sSize) {\n    int left = 0, right = sSize - 1;\n    while (left < right) {\n        char temp = s[left];\n        s[left] = s[right];\n        s[right] = temp;\n        left++;\n        right--;\n    }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">\npublic class Solution {\n    public void reverseString(char[] s) {\n        int left = 0, right = s.length - 1;\n        while (left < right) {\n            char temp = s[left];\n            s[left] = s[right];\n            s[right] = temp;\n            left++;\n            right--;\n        }\n    }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">\nclass Solution(object):\n    def reverseString(self, s):\n        left, right = 0, len(s) - 1\n        while left < right:\n            s[left], s[right] = s[right], s[left]\n            left += 1\n            right -= 1\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"csharp\">\n    <pre><code class=\"language-csharp\">\npublic class Solution {\n    public void ReverseString(char[] s) {\n        int left = 0, right = s.Length - 1;\n        while (left < right) {\n            char temp = s[left];\n            s[left] = s[right];\n            s[right] = temp;\n            left++;\n            right--;\n        }\n    }\n}\n    <\/code><\/pre>\n  <\/div>\n<\/div>\n<!-- \u2705 END: Code Tabs -->\n\n<!-- \u2705 JS Tab Switch Logic -->\n<script>\ndocument.addEventListener('DOMContentLoaded', function () {\n    const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n    const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n    buttons.forEach(button => {\n        button.addEventListener('click', () => {\n            const lang = button.getAttribute('data-lang');\n            buttons.forEach(btn => btn.classList.remove('active'));\n            button.classList.add('active');\n            contents.forEach(content => {\n                content.classList.toggle('active', content.getAttribute('data-lang') === lang);\n            });\n        });\n    });\n});\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1) extra memory. Examples: Example 1: Input: s = [&#8220;h&#8221;,&#8221;e&#8221;,&#8221;l&#8221;,&#8221;l&#8221;,&#8221;o&#8221;] Output: [&#8220;o&#8221;,&#8221;l&#8221;,&#8221;l&#8221;,&#8221;e&#8221;,&#8221;h&#8221;] Example 2: Input: s = [&#8220;H&#8221;,&#8221;a&#8221;,&#8221;n&#8221;,&#8221;n&#8221;,&#8221;a&#8221;,&#8221;h&#8221;] Output: [&#8220;h&#8221;,&#8221;a&#8221;,&#8221;n&#8221;,&#8221;n&#8221;,&#8221;a&#8221;,&#8221;H&#8221;] Approach: Two Pointer Technique Initialize two<\/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":[811,810],"tags":[],"class_list":["post-6154","post","type-post","status-publish","format-standard","category-data-structures-and-algorithms","category-dsa"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6154","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=6154"}],"version-history":[{"count":2,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6154\/revisions"}],"predecessor-version":[{"id":6439,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6154\/revisions\/6439"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}