7
Java - Palindrome String
source link: http://mussatto.github.io/java/string/palindrome/2018/06/22/string-palindrome-java.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Java - Palindrome String
Another common Java exercise is to verify if a String is a Palindrome
The algorithm is:
- Split the String into an array with all the characters
- Reverse the array
- Join the array again
- Check if the reversed string is equal to the input
Reversing the array
public class StringReverser {
public static String reverse(String s1){
if(isNull(s1)){
return null;
}
List<String> charList = asList(s1.split(""));
Collections.reverse(charList);
return String.join("", charList);
}
}
Palindrome class
public class Palindrome {
public static boolean isPalindrome(String s1){
if(isNull(s1)){
return false;
}
String reversed = StringReverser.reverse(s1);
return reversed.equals(s1);
}
}
Source code: Palindrome
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK