Check if the given string is the same as its reflection in a mirror
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
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++
#include <bits/stdc++.h>
using namespace std;
bool isReflectionEqual(string s)
{
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 (symmetric.find(s[i]) == symmetric.end())
return false ;
string rev = s;
reverse(rev.begin(), rev.end());
if (rev == s)
return true ;
else
return false ;
}
int main()
{
string s = "MYTYM" ;
if (isReflectionEqual(s))
cout << "YES" ;
else
cout << "NO" ;
}
|
Java
import java.util.*;
class GFG
{
static String Reverse(String s)
{
char [] charArray = s.toCharArray();
reverse(charArray, 0 , charArray.length - 1 );
return new String(charArray);
}
static boolean isReflectionEqual(String s)
{
HashSet<Character> symmetric = new HashSet<>();
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 (symmetric.contains(s.charAt(i)) == false )
{
return false ;
}
}
String rev = s;
s = Reverse(s);
if (rev.equals(s))
{
return true ;
} else {
return false ;
}
}
static void reverse( char str[], int start, int end)
{
char temp;
while (start <= end)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
public static void main(String[] args)
{
String s = "MYTYM" ;
if (isReflectionEqual(s))
{
System.out.println( "YES" );
}
else
{
System.out.println( "NO" );
}
}
}
|
Python3
def isReflectionEqual(s):
symmetric = dict ()
str1 = "AHIMOTUVWXY"
for i in str1:
symmetric[i] = 1
n = len (s)
for i in range (n):
if (symmetric[s[i]] = = 0 ):
return False
rev = s[:: - 1 ]
if (rev = = s):
return True
else :
return False
s = "MYTYM"
if (isReflectionEqual(s)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic ;
class GFG
{
static string Reverse( string s )
{
char [] charArray = s.ToCharArray();
Array.Reverse( charArray );
return new string ( charArray );
}
static bool isReflectionEqual( string s)
{
HashSet< char > symmetric = new HashSet< char >();
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 (symmetric.Contains(s[i]) == false )
return false ;
string rev = s;
s = Reverse(s);
if (rev == s)
return true ;
else
return false ;
}
static public void Main()
{
string s = "MYTYM" ;
if (isReflectionEqual(s))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
function Reverse(s)
{
let charArray = s.split( "" );
reverse(charArray, 0, charArray.length - 1);
return charArray.join( "" );
}
function isReflectionEqual(s)
{
let symmetric = new Set();
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' );
let n = s.length;
for (let i = 0; i < n; i++)
{
if (symmetric.has(s[i]) == false )
{
return false ;
}
}
let rev = s;
s = Reverse(s);
if (rev==(s))
{
return true ;
} else {
return false ;
}
}
function reverse(str,start,end)
{
let temp;
while (start <= end)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
let s = "MYTYM" ;
if (isReflectionEqual(s))
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
11 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...