Palindrome solving is one of the very beginner question often asked and given by teachers to solve. This can be complicated if you're restricted to use some specific data structure, or using a specific method but it is very simple if you have to solve it with no conditions.


What is palindrome?

Palindrome is a word, string or phrase that reads same forward as well as backwards for example eye, madam, civic, level, refer, 22/2/22, 11:11 are all palindromes.

How to check palindrome? (Video Tutorial)

To check if a word or phrase is palindrome you need to compare original with its reversed version.

Simplest way to do this is given below: or see at github

void main() {

checkPalindrome("eye")? print("Palindrome") : print("Not Palindrome");         checkPalindrome("Eye")? print("Palindrome") : print("Not Palindrome"); checkPalindrome("Bye")? print("Palindrome") : print("Not Palindrome"); checkPalindrome("")? print("Palindrome") : print("Not Palindrome"); 

bool checkPalindrome(String inputString){

return inputString == inputString.split("").reversed.join();

}

Output:
Palindrome Output

Split function converts string to list and reversed will reverse the list and join reconverts list to string. As a result we get reversed string that gets compared with actual string. and true or false is returned. 
It will give minimum time complexity and no for loops. You can replicate same thing in other languages as well. Typically they all have similar functions.


Follow for more on social media. If you found it useful share.