1. Overview
1.概述
When we work in Java, String manipulation and comparison are everyday tasks.
当我们使用 Java 时,字符串操作和比较是日常任务。
In this quick tutorial, let’s delve into an interesting problem: checking if a String equals its mirror reflection.
在本快速教程中,我们将深入探讨一个有趣的问题:检查 String 是否等于其镜像。
2. Introduction to the Problem
2.问题介绍
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“.
一个常见的误解是,获得字符串的镜像只需颠倒其顺序即可。直觉上,我们可能会认为它的镜像是 “LLA”。然而,使用实际的镜子仔细观察后,我们会发现 “LLA”与”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”.
关键的误解在于,字符串中的每个字符在其镜像反映中都会发生反转。因此,“ALL” 的镜像实际上显示为 “⅃⅃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.
根据反转行为,字符可分为对称字符和非对称字符。对称字符在反转时保持不变,如’A’、’O’、’o’、’V’、’v’、’M’、’8’、’+’、’-‘等。相反,非对称字符与其反转形式不同,例如’L’、’S’、’p’、’h’、’/’、’3’等。
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:
为简单起见,本教程中我们将仅以检查由大写英文字母组成的 String 值为例。那么,这些就是我们需要检查的对称大写字符:
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.
接下来,让我们探讨如何执行 String 镜像反射检查。
3. The Idea to Solve the Problem: Combining the Two Checks
3.解决问题的思路:将两项检查结合起来
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
4.实现 containsOnlySymmetricLetters() 方法
As we have defined the symmetric letters in a Set, we need to examine whether SYMMETRIC_LETTERS contains each character in the given String:
由于我们在 Set 中定义了对称字母,因此我们需要检查 SYMMETRIC_LETTERS是否包含给定 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.
接下来,让我们来实现 isPalindrome() 方法。
5. Implementing the isPalindrome() Method
5.实现 isPalindrome() 方法
There are different ways to check if a String is a palindrome in Java. Let’s take a straightforward solution to do the job:
在 Java 中,检查字符串是否是回文字符串的方法多种多样。让我们采用一种直接的解决方案来完成这项工作:
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.
正如我们所看到的,我们检查 input 字符串 和反向 input 是否相等,以确定 input 是否是回文字符串。
6. Testing the Solution
6.测试解决方案
Now, both building blocks of isMirrorImageEqual() are in place. Finally, let’s create a test to verify if this method solves our problem:
现在,isMirrorImageEqual() 的两个构件都已就位。最后,让我们创建一个测试来验证该方法是否解决了我们的问题:
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.
正如我们所看到的,我们使用各种输入 String 值对该方法进行了测试,我们的解决方案如期运行。
7. Conclusion
7.结论
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.
在本文中,我们首先讨论了 String 的镜像反射的特征。然后,我们探讨了检查给定 String 及其镜像是否相等的解决方案。
As always, the complete source code for the examples is available over on GitHub.
一如既往,这些示例的完整源代码可在 GitHub 上获取。