You've successfully subscribed to Nicholas Workshop
Great! Next, complete checkout for full access to Nicholas Workshop
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Palindrome

Nicholas Wong
Nicholas Wong

Definition of Palindrome

left-to-right, and right-to-left, and it’s the same. For examples:

Abba
A man, a plan, a canal - Panama!

Solution

You need 2 pointers, one at the beginning, another one in the end. Move them one by one towards the center and check if it's palindrome.

boolean check(String text) {
	int a = 0;
	int b = text.length - 1;
	while(b > a) {
		if (!isLetter(text[a])) {
			++a;
			continue;
		}
		if (!isLetter(text[b])) {
			--b;
			continue;
		}
		if (!equalsIngoreCase(text[a], text[b]) {
			return false;
		}
		++a;
		--b;
	}
	return true;
}
Algorithm

Nicholas Wong

Fullstack software engineer with strong background in computer science and extensive experience in software engineering and architecture. Studied in NYU, worked in Yahoo, Rakuten and Manulife.