Shortest code to check palindrome (Using Dart)

Shortest code to check palindrome (Using Dart)

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

What is palindrome?

A palindrome is a word, string, or phrase that reads the same forward as well as backward 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 the original with its reversed version.

The simplest way to do this is given below: or check it out 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

The split function converts the string to a list and reversed will reverse the list and join reconverts the list to a string. As a result, we get a reversed string that gets compared with the actual string. and true or false is returned. It will give minimum time complexity and no for loops. You can replicate the 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

About Author

I’m a Flutter developer with nearly 3 years of experience, specializing in building mobile applications. I have expertise in backend technologies like Node.js and MySQL, enabling me to create full-stack solutions. As the author of this blog, I focus on sharing practical insights into app development, backend integration, and modern software practices. My goal is to deliver informative and actionable content for developers, tech enthusiasts, and businesses. With a passion for clean code and scalable architecture, I’m committed to helping others build reliable and efficient applications through this platform.