Related Articles
Check if the given string is the same as its reflection in a mirror
• Last Updated : 19 Mar, 2019

Given a string S containing only uppercase English characters. The task is to find whether S is the same as its reflection in a mirror.

Examples:

```Input: str = "AMA"
Output: YES
AMA is same as its reflection in the mirror.

Input: str = "ZXZ"
Output: NO
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The string obviously has to be a palindrome, but that alone is not enough. All characters in the string should be symmetric so that their reflection is also the same. The symmetric characters are AHIMOTUVWXY.

• Store the symmetric characters in an unordered_set.
• Traverse the string and check if there is any non-symmetric character present in the string. If yes then return false.
• Else check if the string is palindrome or not. If the string is palindrome also then return true else return false.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the ``// above approach`` ` `#include ``using` `namespace` `std;`` ` `// Function to check reflection``bool` `isReflectionEqual(string s)``{``    ``// Symmetric characters``    ``unordered_set<``char``> symmetric = { ``'A'``, ``'H'``, ``'I'``, ``'M'``,``                        ``'O'``, ``'T'``, ``'U'``, ``'V'``, ``'W'``, ``'X'``, ``'Y'` `};`` ` `    ``int` `n = s.length();`` ` `    ``for` `(``int` `i = 0; i < n; i++)``        ``// If any non-symmetric character is``        ``// present, the answer is NO``        ``if` `(symmetric.find(s[i]) == symmetric.end())``            ``return` `false``;`` ` `    ``string rev = s;``    ``reverse(rev.begin(), rev.end());`` ` `    ``// Check if the string is a palindrome``    ``if` `(rev == s)``        ``return` `true``;``    ``else``        ``return` `false``;``}`` ` `// Driver code``int` `main()``{``    ``string s = ``"MYTYM"``;``    ``if` `(isReflectionEqual(s))``        ``cout << ``"YES"``;``    ``else``        ``cout << ``"NO"``;``}`

## Java

 `// Java implementation of above approach``import` `java.util.*;`` ` `class` `GFG ``{`` ` `    ``static` `String Reverse(String s)``    ``{``        ``char``[] charArray = s.toCharArray();``        ``reverse(charArray, ``0``, charArray.length - ``1``);``        ``return` `new` `String(charArray);``    ``}`` ` `    ``// Function to check reflection ``    ``static` `boolean` `isReflectionEqual(String s) ``    ``{``        ``HashSet symmetric = ``new` `HashSet<>();`` ` `        ``// Symmetric characters ``        ``symmetric.add(``'A'``);``        ``symmetric.add(``'H'``);``        ``symmetric.add(``'I'``);``        ``symmetric.add(``'M'``);``        ``symmetric.add(``'O'``);``        ``symmetric.add(``'T'``);``        ``symmetric.add(``'U'``);``        ``symmetric.add(``'V'``);``        ``symmetric.add(``'W'``);``        ``symmetric.add(``'X'``);``        ``symmetric.add(``'Y'``);`` ` `        ``int` `n = s.length();`` ` `        ``// If any non-symmetric character is``        ``for` `(``int` `i = ``0``; i < n; i++) ``         ` `        ``// present, the answer is NO ``        ``{``            ``if` `(symmetric.contains(s.charAt(i)) == ``false``) ``            ``{``                ``return` `false``;``            ``}``        ``}`` ` `        ``String rev = s;``        ``s = Reverse(s);`` ` `        ``// Check if the String is a palindrome ``        ``if` `(rev.equals(s)) ``        ``{``            ``return` `true``;``        ``} ``else` `{``            ``return` `false``;``        ``}``    ``}``     ` `    ``// Reverse the letters of the word ``    ``static` `void` `reverse(``char` `str[], ``int` `start, ``int` `end) ``    ``{`` ` `        ``// Temporary variable to store character ``        ``char` `temp;``        ``while` `(start <= end)``        ``{``            ``// Swapping the first and last character ``            ``temp = str[start];``            ``str[start] = str[end];``            ``str[end] = temp;``            ``start++;``            ``end--;``        ``}``    ``}``     ` `    ``// Driver code ``    ``public` `static` `void` `main(String[] args) ``    ``{``        ``String s = ``"MYTYM"``;``        ``if` `(isReflectionEqual(s))``        ``{``            ``System.out.println(``"YES"``);``        ``} ``        ``else` `        ``{``            ``System.out.println(``"NO"``);``        ``}``    ``}``}`` ` `// This code contributed by Rajput-Ji`

## Python3

 `# Python3 implementation of the ``# above approach`` ` `# Function to check reflection``def` `isReflectionEqual(s):`` ` `    ``# Symmetric characters``    ``symmetric ``=` `dict``()`` ` `    ``str1 ``=` `"AHIMOTUVWXY"`` ` `    ``for` `i ``in` `str1:``        ``symmetric[i] ``=` `1`` ` `    ``n ``=` `len``(s)`` ` `    ``for` `i ``in` `range``(n):``         ` `        ``# If any non-symmetric character ``        ``# is present, the answer is NO``        ``if` `(symmetric[s[i]] ``=``=` `0``):``            ``return` `False`` ` `    ``rev ``=` `s[::``-``1``]`` ` `    ``# Check if the is a palindrome``    ``if` `(rev ``=``=` `s):``        ``return` `True``    ``else``:``        ``return` `False`` ` `# Driver Code``s ``=` `"MYTYM"``if` `(isReflectionEqual(s)):``    ``print``(``"YES"``)``else``:``    ``print``(``"NO"``)`` ` `# This code is contributed by Mohit Kumar`

## C#

 `// C# implementation of the above approach ``using` `System;``using` `System.Collections.Generic ;`` ` `class` `GFG``{`` ` `    ``static` `string` `Reverse( ``string` `s )``    ``{``        ``char``[] charArray = s.ToCharArray();``        ``Array.Reverse( charArray );``        ``return` `new` `string``( charArray );``    ``}``     ` `    ``// Function to check reflection ``    ``static` `bool` `isReflectionEqual(``string` `s) ``    ``{ ``        ``HashSet<``char``> symmetric = ``new` `HashSet<``char``>();``         ` `        ``// Symmetric characters ``        ``symmetric.Add(``'A'``);``        ``symmetric.Add(``'H'``);``        ``symmetric.Add(``'I'``);``        ``symmetric.Add(``'M'``);``        ``symmetric.Add(``'O'``);``        ``symmetric.Add(``'T'``);``        ``symmetric.Add(``'U'``);``        ``symmetric.Add(``'V'``);``        ``symmetric.Add(``'W'``);``        ``symmetric.Add(``'X'``);``        ``symmetric.Add(``'Y'``);``     ` `        ``int` `n = s.Length; ``     ` `        ``for` `(``int` `i = 0; i < n; i++) ``         ` `            ``// If any non-symmetric character is ``            ``// present, the answer is NO ``            ``if` `(symmetric.Contains(s[i]) == ``false``) ``                ``return` `false``; ``     ` `        ``string` `rev = s; ``        ``s = Reverse(s); ``     ` `        ``// Check if the string is a palindrome ``        ``if` `(rev == s) ``            ``return` `true``; ``        ``else``            ``return` `false``; ``    ``} `` ` `    ``// Driver code ``    ``static` `public` `void` `Main() ``    ``{ ``        ``string` `s = ``"MYTYM"``; ``        ``if` `(isReflectionEqual(s)) ``            ``Console.WriteLine(``"YES"``); ``        ``else``            ``Console.WriteLine(``"NO"``); ``    ``} ``}`` ` `// This code is contributed by Ryuga`
Output:
```YES
```

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up