beginner · two pointers · strings

Does the phrase read the same both ways?

Problem

A phrase contains lowercase letters and spaces. Ignoring the spaces, does it read the same forwards and backwards?

Constraints

  • 0 ≤ length ≤ 32
  • characters: a–z and space

Examples

"never odd or even" → true

"abca" → false

Show the solution

Two pointers that skip spaces

Compare the first and last letters, then move inward; a space on either side is skipped.

Why it is correct

Each comparison checks a mirrored pair of letters of the cleaned phrase; all pairs match exactly when it is a palindrome.

Complexity

Time O(n), extra space O(1). Every step moves at least one pointer inward.

Edge cases

  • Empty string or only spaces: true.
  • Odd length: the middle letter is never compared.

Common mistakes

  • Comparing spaces as letters.
  • Copying and reversing the string when O(1) space is asked for.

Step through it

The solution in four languages

Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.

namespace Bekadan.Dsa.Examples.Problems;
 
public static class ValidPalindrome
{
    /// <summary>Whether the phrase reads the same both ways, ignoring spaces. O(n), O(1) space.</summary>
    public static bool Check(string phrase)
    {
        int left = 0, right = phrase.Length - 1;
        while (left < right)
        {
            if (phrase[left] == ' ') { left++; continue; }
            if (phrase[right] == ' ') { right--; continue; }
            if (phrase[left] != phrase[right]) return false;
            left++;
            right--;
        }
        return true;
    }
}

Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/ValidPalindrome.cs

def check(phrase: str) -> bool:
    """Whether the phrase reads the same both ways, ignoring spaces. O(n), O(1) space."""
    left, right = 0, len(phrase) - 1
    while left < right:
        if phrase[left] == " ":
            left += 1
            continue
        if phrase[right] == " ":
            right -= 1
            continue
        if phrase[left] != phrase[right]:
            return False
        left, right = left + 1, right - 1
    return True

Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/valid_palindrome.py

package dev.bekadan.dsa.problems;
 
public final class ValidPalindrome {
    private ValidPalindrome() {
    }
 
    /** Whether the phrase reads the same both ways, ignoring spaces. O(n), O(1) space. */
    public static boolean check(String phrase) {
        int left = 0, right = phrase.length() - 1;
        while (left < right) {
            if (phrase.charAt(left) == ' ') {
                left++;
                continue;
            }
            if (phrase.charAt(right) == ' ') {
                right--;
                continue;
            }
            if (phrase.charAt(left) != phrase.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}

Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/ValidPalindrome.java

#pragma once
 
#include <cstddef>
#include <string_view>
 
namespace bekadan::dsa::problems {
 
// Whether the phrase reads the same both ways, ignoring spaces. O(n), O(1) space.
inline bool valid_palindrome(std::string_view phrase) {
    std::ptrdiff_t left = 0, right = std::ssize(phrase) - 1;
    const auto at = [&](std::ptrdiff_t i) { return phrase[static_cast<std::size_t>(i)]; };
    while (left < right) {
        if (at(left) == ' ') { ++left; continue; }
        if (at(right) == ' ') { --right; continue; }
        if (at(left) != at(right)) return false;
        ++left;
        --right;
    }
    return true;
}
 
}  // namespace bekadan::dsa::problems

Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/valid_palindrome.hpp