Learn-dsa..in 30 days!



























CC-2 : Check if String is palindrome.

Description:

Given an input String check if it is a palindrome.

Test cases and expected outputs:

Input Parameters Expected outputs
Original String: reviver IsPalindrome: true
Original String: noob IsPalindrome: false

Pseudocode:

isPalindrome(str):

The String to be checked is received as input parameter.
If str.length()==1, return true.
If character at first index is not same as character at last index, return false.
Recursively call method isPalindrome() after removing first and last characters of current String as first and last characters of current String have been processed above.

Code:

public boolean isPalindrome(String str) {
	if (str.length() <= 1) {
		return true;
	}else {
		if (str.charAt(0) != str.charAt(str.length()-1)) {
			return false;
		}		
	}
	return isPalindrome(str.substring(1, str.length()-1));
}

Click here to download and run code and test cases !