Strings · Easy

47. Check Whether a String Is a Palindrome

Topics: String

Solve it on DeVert Campus - free code runner

Problem

Given a string s, print 'Yes' if it reads the same forwards and backwards, otherwise print 'No'. Comparison is case-sensitive and exact (no characters are ignored).

Input and output

Input: The input is a single line containing the string s. Output: Print 'Yes' if s is a palindrome, otherwise print 'No'.

Example

Input:
madam
Output:
Yes

Constraints

1 <= |s| <= 10^5

Key observation

A string of length 1 (or an empty string) is trivially always a palindrome - a quick mental sanity check worth running before trusting any more complex logic.

Intuition

Check whether a string reads exactly the same forwards and backwards. Brute force: Build the fully reversed string and compare it to the original for equality - correct, but uses O(n) extra space to build the reversed copy when a two-pointer check needs none. Optimised: Comparing from both ends inward and stopping at the FIRST mismatch avoids ever needing to build a reversed copy - and it can exit early the instant a mismatch is found, without scanning the rest of the string.

Optimal - Two Pointer with Early Exit

Use the same converging two-pointer scan, but return 'No' immediately the instant a mismatched pair of characters is found instead of continuing to check the rest of the string. In the best and average case this terminates long before reaching the middle, and even in the worst case (an actual palindrome) it only ever needs to inspect each character once, up to the midpoint. This is the provably optimal approach: no correct algorithm can avoid looking at every character in the worst case, and this uses no extra memory beyond the two pointers while doing the least possible work whenever a mismatch exists. Time: O(n) Space: O(1)

Better - Two Pointer, Full Pass

Use two pointers, one starting at the front and one at the back of the string, moving them toward each other and comparing the characters they point to at each step, accumulating whether every pair matched. This avoids ever constructing a second reversed string, cutting the extra memory down to a couple of index variables, though as written it still compares every pair down to the middle even after any mismatch is already known. Time: O(n) Space: O(1)

Brute Force - Reverse and Compare

Build an entirely new string that is the reverse of the input (by iterating from the last character to the first, or using a built-in reverse), then compare this reversed copy to the original string for exact equality, printing 'Yes' if they match and 'No' otherwise. This is correct and simple to reason about, but it always allocates a full second copy of the string even though a palindrome check doesn't actually require ever materializing the reverse. Time: O(n) Space: O(n)

Step by step

1. Point two fingers at the first and last characters. 2. Compare them - if they differ, it's immediately NOT a palindrome, stop and answer No. 3. If they match, move both fingers one step inward and repeat. 4. If the fingers meet or cross without ever finding a mismatch, it IS a palindrome.

Complexity

Time: O(n) in the worst case (a genuine palindrome, or a mismatch only near the middle) - but often faster in practice due to early exit on a mismatch. Space: O(1) - just two pointer positions, no extra string built.

Edge cases

A single-character string (trivially 'Yes'); a string that would be a palindrome if case were ignored but fails the exact case-sensitive comparison ('No'); s up to 10^5 characters for performance.

Interview tip

Be precise that this problem is explicitly CASE-SENSITIVE and exact per its own statement - many similar-sounding palindrome problems elsewhere ask you to ignore case/punctuation/spaces first, so always double-check the exact rules stated.

All DSA problems