Check if a given string is Even-Odd Palindrome or not in Java

Determining if a given string is an even-odd palindrome is the task at sight for a given string str. When the characters at even indices make a palindrome, and the characters at odd indices form a palindrome independently, the string is said to be an even-odd palindrome.

Example 1:

Input:

String str = "zyyxzyyxzy"

Output:

Yes, the given string is Even-Odd Palindrome.

Explanation:

For the given string "zyyxzyyxzy", the string that is formed by the characters at odd indices is "zyzyz" which is an odd-indexed palindrome. The string that is formed by the characters at even indices is "yxyxy" which is an even-indexed palindrome. Therefore, the given string is an Even-Odd Palindrome.

Example 2:

Input:

String str = "aba"

Output:

Yes, the given string is Even-Odd Palindrome.

Explanation:

For the given string "aba", the string that is formed by the characters at odd indices is "aa," which is an odd-indexed palindrome. The string that is formed by the characters at even indices is "b," which is an even-indexed palindrome. Therefore, the given string is an Even-Odd Palindrome.

Example 3:

Input:

String str = "acbba"

Output:

No, the given string is not Even-Odd Palindrome.

Explanation:

For the given string "acbba", the string that is formed by the characters at odd indices is "aba," which is an odd-indexed palindrome. The string that is formed by the characters at even indices is "ba" which is not an even-indexed palindrome. Therefore, the given string is not an Even-Odd Palindrome.

Approach: Naïve Approach

Algorithm:

Step 1: Initialize the two pointers, l, and h, which start at the beginning and end of the string, respectively.

Step 2: Continue looping as long as h exceeds l.

Step 2.1: Verify that the characters at places l and h are the same for each iteration.

Step 2.2: Return false (the string is not a palindrome) if they are not the same.

Step 2.3: Return true (the string is a palindrome) if every character matches symmetrically.

Step 3: Create an empty string called odd_palin.

Step 3.1: Begin a loop through the input string from index 1, work through it up to the second character, and increment the index by two each time.

Step 3.2: Add each character to odd_palin at the odd index.

Step 3.3: Give back the odd_palin string that was created.

Step 4: Initialize even_palin, an empty string.

Step 4.1: Begin a loop through the input string at index 0 (the first character), increasing the index by 2 for each run.

Step 4.2: Add each character to even_palin at the even index.

Step 4.3: Give back the even_palin string that was created.

Step 5: Determine whether the even-indexed string and the odd-indexed string are both palindromes by using the checkPalindrome function.

Step 5.1: Print "Yes, the given string is Even-Odd Palindrome" if both strings are palindromic.

Step 5.2: Print "No, the given string is not Even-Odd Palindrome" if either string is not palindromic.

Implementation:

FileName: EvenOddPalindrome.java

Output

Yes, the given string is Even-Odd Palindrome

Complexity Analysis:

The Time Complexity of the above code is O(N), where 'N' represents the length of the given string and the space complexity is O(N).