Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

When we work in Java, String manipulation and comparison are everyday tasks.

In this quick tutorial, let’s delve into an interesting problem: checking if a String equals its mirror reflection.

2. Introduction to the Problem

It’s a common misconception that obtaining the mirror reflection of a String simply involves reversing its order. Take, for instance, the String “ALL”. Intuitively, one might expect its mirror reflection to be “LLA”. Yet, upon closer examination using an actual mirror, we discover that “LLA” doesn’t quite match the mirrored version of “ALL“.

The key misunderstanding lies in the fact that every individual character within a String undergoes a reversal in its mirror reflection. Thus, the mirror reflection of “ALL” actually appears as “⅃⅃A”.

Characters can be categorized as symmetric or asymmetric based on reversed behavior. A symmetric character is one that remains unchanged when reversed, such as ‘A’, ‘O’, ‘o’, ‘V’, ‘v’, ‘M’, ‘8’, ‘+’, ‘-‘, and so on. Conversely, an asymmetric character differs from its reversed form, exemplified by ‘L’, ‘S’, ‘p’, ‘h’, ‘/’, ‘3’, etc.

So, when we say a String equals its mirror reflection, it has two requirements:

  • The String only contains symmetric characters.
  • The given String must equal its reversed value. In other words, the String must be a palindrome, for example “MUM

For simplicity, we’ll only check String values consisting of uppercase English letters as examples in this tutorial. Then, these are the symmetric uppercase characters we need to check:

final static Set<Character> SYMMETRIC_LETTERS = Set.of('A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y');

Next, let’s explore how to perform the String mirror reflection check.

3. The Idea to Solve the Problem: Combining the Two Checks

Now that we understand the problem’s two requirements, a straightforward idea to solve the problem is creating a method to combine the symmetric character check and palindrome String check:

boolean isMirrorImageEqual(String input) {
    return containsOnlySymmetricLetters(input) && isPalindrome(input);
}

Next, let’s tackle this step by step.

4. Implementing the containsOnlySymmetricLetters() Method

As we have defined the symmetric letters in a Set, we need to examine whether SYMMETRIC_LETTERS contains each character in the given String:

boolean containsOnlySymmetricLetters(String input) {
    Set<Character> characterSet = input.chars()
      .mapToObj(c -> (char) c)
      .collect(Collectors.toSet());
    characterSet.removeAll(SYMMETRIC_LETTERS);
    return characterSet.isEmpty();
}

As the code above shows, we perform this check in three steps:

  • Convert the input String into a Set<Character> named characterSet
  • Remove all required symmetric letters from characterSet
  • Check if characterSet is empty after the removal
  • If the set is empty, it means it only contained symmetric characters

Next, let’s implement the isPalindrome() method.

5. Implementing the isPalindrome() Method

There are different ways to check if a String is a palindrome in Java. Let’s take a straightforward solution to do the job:

boolean isPalindrome(String input) {
    String reversed = new StringBuilder(input).reverse()
      .toString();
    return input.equals(reversed);
}

As we can see, we check if the input String and the reversed input are equal to determine whether input is a palindrome.

6. Testing the Solution

Now, both building blocks of isMirrorImageEqual() are in place. Finally, let’s create a test to verify if this method solves our problem:

assertFalse(isMirrorImageEqual("LOL"));
assertFalse(isMirrorImageEqual("AXY"));
assertFalse(isMirrorImageEqual("HUHU"));
 
assertTrue(isMirrorImageEqual(""))
assertTrue(isMirrorImageEqual("AAA"));
assertTrue(isMirrorImageEqual("HUH"));
assertTrue(isMirrorImageEqual("HIMMIH"));
assertTrue(isMirrorImageEqual("HIMIH"));

As we can see, we test the method with various input String values, and our solution works as expected.

7. Conclusion

In this article, we first discussed the characteristics of a String‘s mirror reflection. Then, we explored a solution to check whether a given String and its mirror reflection are equal.

As always, the complete source code for the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.