Palindrome detection: Difference between revisions

→‎{{header|Java}}: Added a more efficient non-recursive Java solution that also handles upper-plane Unicode characters.
No edit summary
(→‎{{header|Java}}: Added a more efficient non-recursive Java solution that also handles upper-plane Unicode characters.)
Line 2,489:
return testMe.equals(sb.reverse().toString());
}</lang>
 
'''Non-Recursive using indexes (supports upper-plane Unicode)'''
<lang java>public static boolean isPalindrome(String input) {
for (int i = 0, j = input.length() - 1; i < j; i++, j--) {
char startChar = input.charAt(i);
char endChar = input.charAt(j);
 
// Handle surrogate pairs in UTF-16
if (Character.isLowSurrogate(endChar)) {
if (startChar != input.charAt(--j)) {
return false;
}
if (input.charAt(++i) != endChar) {
return false;
}
} else if (startChar != endChar) {
return false;
}
}
return true;
}</lang>
 
'''Recursive (this version does not work correctly with upper-plane Unicode)'''
 
Anonymous user