mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
Fixed error in Palindrome.java and gave semantic names to functions (#3643)
* fixed error and changed functions names fixed error at line 57 and gave semantic names to functions with comments * renamed functions renamed functions to match with original functions' names in the file * Updated TestCases Updated TestCases and changed a function name * Removed main() and changed function name Removed main() and changed the function name from isPalindromeStringBuilder to isPalindrome * fixed typo Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
This commit is contained in:

committed by
GitHub

parent
acf7a86b96
commit
bd267bb7d8
@ -6,26 +6,7 @@ package com.thealgorithms.strings;
|
|||||||
class Palindrome {
|
class Palindrome {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Driver Code
|
* Check if a string is palindrome string or not using String Builder
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
String[] palindromes = { null, "", "aba", "123321" };
|
|
||||||
for (String s : palindromes) {
|
|
||||||
assert isPalindrome(s) &&
|
|
||||||
isPalindromeRecursion(s) &&
|
|
||||||
isPalindrome1(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
String[] notPalindromes = { "abb", "abc", "abc123" };
|
|
||||||
for (String s : notPalindromes) {
|
|
||||||
assert !isPalindrome(s) &&
|
|
||||||
!isPalindromeRecursion(s) &&
|
|
||||||
!isPalindrome1(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a string is palindrome string or not
|
|
||||||
*
|
*
|
||||||
* @param s a string to check
|
* @param s a string to check
|
||||||
* @return {@code true} if given string is palindrome, otherwise
|
* @return {@code true} if given string is palindrome, otherwise
|
||||||
@ -54,17 +35,17 @@ class Palindrome {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return isPalindrome(s.substring(1, s.length() - 1));
|
return isPalindromeRecursion(s.substring(1, s.length() - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a string is palindrome string or not another way
|
* Check if a string is palindrome string or not using two pointer technique
|
||||||
*
|
*
|
||||||
* @param s a string to check
|
* @param s a string to check
|
||||||
* @return {@code true} if given string is palindrome, otherwise
|
* @return {@code true} if given string is palindrome, otherwise
|
||||||
* {@code false}
|
* {@code false}
|
||||||
*/
|
*/
|
||||||
public static boolean isPalindrome1(String s) {
|
public static boolean isPalindromeTwoPointer(String s) {
|
||||||
if (s == null || s.length() <= 1) {
|
if (s == null || s.length() <= 1) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,20 @@ public class PalindromeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void palindrome() {
|
public void palindrome() {
|
||||||
String input1 = "kayak";
|
|
||||||
String input2 = "kayaks";
|
String[] palindromes = { null, "", "aba", "123321", "kayak" };
|
||||||
Assertions.assertTrue(Palindrome.isPalindrome(input1));
|
for (String s : palindromes) {
|
||||||
Assertions.assertFalse(Palindrome.isPalindrome(input2));
|
Assertions.assertTrue(Palindrome.isPalindrome(s) &&
|
||||||
Assertions.assertTrue(Palindrome.isPalindromeRecursion(input1));
|
Palindrome.isPalindromeRecursion(s) &&
|
||||||
Assertions.assertFalse(Palindrome.isPalindromeRecursion(input2));
|
Palindrome.isPalindromeTwoPointer(s));
|
||||||
Assertions.assertTrue(Palindrome.isPalindrome1(input1));
|
}
|
||||||
Assertions.assertFalse(Palindrome.isPalindrome1(input2));
|
|
||||||
|
String[] notPalindromes = { "abb", "abc", "abc123", "kayaks" };
|
||||||
|
for (String s : notPalindromes) {
|
||||||
|
Assertions.assertFalse(Palindrome.isPalindrome(s) ||
|
||||||
|
Palindrome.isPalindromeRecursion(s) ||
|
||||||
|
Palindrome.isPalindromeTwoPointer(s));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user