- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPalindrome.java
29 lines (24 loc) · 816 Bytes
/
Palindrome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
importjava.util.Scanner;
importjava.util.*;
importjava.io.*;
classMain {
publicstaticStringPalindrome(Stringstr) {
// code goes here
// Remove non-alphanumeric characters
StringstrProcessed = str.replaceAll("[^a-zA-Z0-9]", "");
// Check for inequality in pairs
intstrProcessedLength = strProcessed.length();
for (intindex = 0; index <= strProcessedLength / 2; index++) {
if (strProcessed.charAt(index) != strProcessed.charAt(strProcessedLength - 1 - index)) {
return"false";
}
}
// If no inequality, must be palindrome
return"true";
}
publicstaticvoidmain (String[] args) {
// keep this function call here
Scanners = newScanner(System.in);
System.out.print(Palindrome(s.nextLine()));
}
}