Algorithms and Data Structures · Junior · Question 43 of 75

How do you check whether a string is a palindrome in O(n) time and O(1) extra space?

Explanation

Two pointers compare s[i] with s[j] and stop at the first mismatch. Reversing works too but creates a new string, so it uses O(n) memory. Sorting is wrong: it loses the order of characters.

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 43

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz