{"id":267,"date":"2025-06-09T13:55:17","date_gmt":"2025-06-09T08:25:17","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=267"},"modified":"2025-06-09T13:55:18","modified_gmt":"2025-06-09T08:25:18","slug":"palindrome-string-recursive-iterative-solution","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/","title":{"rendered":"Palindrome String | Recursive &amp; Iterative Python Solutions"},"content":{"rendered":"\n<p>Learn two easy ways to test a Palindrome String: a clean recursive method and a space saving two-pointer loop. Includes code with comments, step-by-step explanation, dry run, and Big-O analysis.<\/p>\n\n\n\n<p>The<strong> [<a href=\"https:\/\/www.geeksforgeeks.org\/problems\/palindrome-string0817\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a>]<\/strong> is given here for your quick reference.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-d7fb988e-514c-4980-b80a-4fd054e0711e\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#0-1-what-does-the-problem-ask\" style=\"\">1 What does the problem ask?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#1-2-examples\" style=\"\">2 Examples<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#2-3-intuition-amp-approach\" style=\"\">3 Intuition &amp; Approach<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#3-key-idea-for-both-methods\" style=\"\">Key idea for both methods<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#4-4-code-1-%E2%80%93-recursive-unchanged-lines-comments-added\" style=\"\">4 Code 1 \u2013 Recursive (unchanged lines, comments added)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#5-5-code-2-%E2%80%93-iterative-two-pointers-unchanged-lines-comments-added\" style=\"\">5 Code 2 \u2013 Iterative Two Pointers (unchanged lines, comments added)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#6-6-code-walk-through\" style=\"\">6 Code Walk-through<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#7-recursive-method\" style=\"\">Recursive method<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#8-iterative-method\" style=\"\">Iterative method<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#9-7-dry-run-iterative-method-on-abba-\" style=\"\">7 Dry Run (iterative method on &#8220;abba&#8221;)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#10-8-complexity\" style=\"\">8 Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-string-recursive-iterative-solution\/#11-9-conclusion\" style=\"\">9 Conclusion<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-1-what-does-the-problem-ask\">What does the problem ask?<\/h2>\n\n\n\n<p>Given a string <code>s<\/code>, tell whether it reads the same forwards and backwards.<br>Return <strong><code>True<\/code><\/strong> if it is a palindrome, or <strong><code>False<\/code><\/strong> otherwise.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-2-examples\">Examples<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Input<\/th><th>Output<\/th><th>Reason<\/th><\/tr><\/thead><tbody><tr><td><code>\"abba\"<\/code><\/td><td><code>True<\/code><\/td><td>Same letters from both ends.<\/td><\/tr><tr><td><code>\"racecar\"<\/code><\/td><td><code>True<\/code><\/td><td>Reads the same both ways.<\/td><\/tr><tr><td><code>\"abc\"<\/code><\/td><td><code>False<\/code><\/td><td><code>a<\/code> \u2260 <code>c<\/code>.<\/td><\/tr><tr><td><code>\"a\"<\/code><\/td><td><code>True<\/code><\/td><td>One letter is always a palindrome.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-3-intuition-amp-approach\">Intuition &amp; Approach<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-key-idea-for-both-methods\">Key idea for both methods<\/h3>\n\n\n\n<p>Compare matching characters from the left and right ends and move inwards:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If every pair matches \u2192 palindrome.<\/li>\n\n\n\n<li>If any pair differs \u2192 not a palindrome.<\/li>\n<\/ul>\n\n\n\n<p>We show two ways to do this:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Recursive<\/strong> \u2013 a clean self-calling check.<\/li>\n\n\n\n<li><strong>Iterative (two pointers)<\/strong> \u2013 a simple <code>while<\/code> loop.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-4-code-1-%E2%80%93-recursive-unchanged-lines-comments-added\">Code 1 \u2013 Recursive<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    # helper that checks s[left .. right]\n    def solve(self, s, left, right):\n        if left &gt;= right:        # crossed over \u2192 all pairs matched\n            return True\n        if s[left] != s[right]:  # mismatch\n            return False\n        # move both pointers inward\n        return self.solve(s, left + 1, right - 1)\n\n    def isPalindrome(self, s: str) -&gt; bool:\n        # check the whole string\n        return self.solve(s, 0, len(s) - 1)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># helper that checks s[left .. right]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">solve<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">s<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">left<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">right<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> left &gt;= right:        <\/span><span style=\"color: #6A9955\"># crossed over \u2192 all pairs matched<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> s[left] != s[right]:  <\/span><span style=\"color: #6A9955\"># mismatch<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># move both pointers inward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.solve(s, left + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, right - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">isPalindrome<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">s<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #4EC9B0\">bool<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># check the whole string<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.solve(s, <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(s) - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-5-code-2-%E2%80%93-iterative-two-pointers-unchanged-lines-comments-added\">Code 2 \u2013 Iterative Two Pointers <\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def isPalindrome(self, s: str) -&gt; bool:\n        n = len(s)\n        left = 0\n        right = n - 1\n        while left &lt; right:\n            if s[left] != s[right]:   # mismatch\n                return False\n            left += 1                 # move inward\n            right -= 1\n        return True                   # all pairs matched\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">isPalindrome<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">s<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #4EC9B0\">bool<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(s)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        left = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        right = n - <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> left &lt; right:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> s[left] != s[right]:   <\/span><span style=\"color: #6A9955\"># mismatch<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            left += <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">                 <\/span><span style=\"color: #6A9955\"># move inward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            right -= <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">                   <\/span><span style=\"color: #6A9955\"># all pairs matched<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-6-code-walk-through\">Code Walkthrough<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-recursive-method\">Recursive method<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Base cases<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>left &gt;= right<\/code> \u2192 checked all pairs \u2192 return <code>True<\/code>.<\/li>\n\n\n\n<li>Characters differ \u2192 return <code>False<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Recursive step<\/strong><br>Move <code>left<\/code> rightward and <code>right<\/code> leftward and call again.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-iterative-method\">Iterative method<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set two pointers: <code>left<\/code> at start, <code>right<\/code> at end.<\/li>\n\n\n\n<li>While they have not crossed:\n<ul class=\"wp-block-list\">\n<li>If characters differ \u2192 not a palindrome.<\/li>\n\n\n\n<li>Else move both pointers inward.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Loop ends without a mismatch \u2192 palindrome.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-7-dry-run-iterative-method-on-abba-\">Dry Run (iterative method on <code>\"abba\"<\/code>)<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>left<\/th><th>right<\/th><th><code>s[left]<\/code><\/th><th><code>s[right]<\/code><\/th><th>Action<\/th><\/tr><\/thead><tbody><tr><td>0<\/td><td>3<\/td><td><code>a<\/code><\/td><td><code>a<\/code><\/td><td>match \u2192 move inward<\/td><\/tr><tr><td>1<\/td><td>2<\/td><td><code>b<\/code><\/td><td><code>b<\/code><\/td><td>match \u2192 move inward<\/td><\/tr><tr><td>2<\/td><td>1<\/td><td>\u2013<\/td><td>\u2013<\/td><td>pointers crossed \u2192 stop<\/td><\/tr><tr><td><strong>Return<\/strong><\/td><td><\/td><td><\/td><td><\/td><td><code>True<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10-8-complexity\">Complexity<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Method<\/th><th>Time<\/th><th>Space<\/th><\/tr><\/thead><tbody><tr><td>Recursive<\/td><td><strong>O(n)<\/strong><\/td><td><strong>O(n)<\/strong> \u2013 call stack can be <code>n\/2<\/code> deep<\/td><\/tr><tr><td>Iterative<\/td><td><strong>O(n)<\/strong><\/td><td><strong>O(1)<\/strong> \u2013 only pointers<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><em><code>n<\/code> is the length of the string.<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"11-9-conclusion\">Conclusion<\/h2>\n\n\n\n<p>A palindrome check needs only a linear scan from both ends.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>recursive<\/strong> version is short and clear but uses extra stack space.<\/li>\n\n\n\n<li>The <strong>iterative<\/strong> two-pointer version saves space and is often preferred in practice.<\/li>\n<\/ul>\n\n\n\n<p>Choose the one that best fits your style or memory limits.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">Join our FREE Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn two easy ways to test a Palindrome String: a clean recursive method and a space saving two-pointer loop. Includes code with comments, step-by-step explanation, dry run, and Big-O analysis. The [Problem Link] is given here for your quick reference. What does the problem ask? Given a string s, tell whether it reads the same<\/p>\n","protected":false},"author":1,"featured_media":268,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","footnotes":""},"categories":[3,4],"tags":[8,11,22],"class_list":{"0":"post-267","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-easy","10":"tag-recursion","11":"tag-strings"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/palindrome-string-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=267"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/267\/revisions"}],"predecessor-version":[{"id":270,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/267\/revisions\/270"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/268"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}