Last active
March 30, 2022 11:16
-
-
Save ricky-lim/c5f0caf826d2ee9297d65a582235bbc8 to your computer and use it in GitHub Desktop.
Palindrom checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.stream.IntStream; | |
class Scratch { | |
public static boolean palindromeChecker(String s) { | |
String normalizedString = s.trim().toLowerCase(); | |
return IntStream.range(0, normalizedString.length() / 2) | |
.allMatch(i -> | |
normalizedString.charAt(i) == | |
normalizedString.charAt(normalizedString.length() - i - 1)); | |
} | |
public static void main(String[] args) { | |
// Palindrome | |
String s1 = "civic"; | |
String s2 = "maDam"; | |
String s3 = "aBBa"; | |
// Non-palindrome | |
String s4 = "camCard"; | |
String s5 = "Ricky"; | |
System.out.println(palindromeChecker(s1)); | |
System.out.println(palindromeChecker(s2)); | |
System.out.println(palindromeChecker(s3)); | |
System.out.println(palindromeChecker(s4)); | |
System.out.println(palindromeChecker(s5)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment