Open In App

Python Dictionary to find mirror characters in a string

Last Updated : 28 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string and a number N, we need to mirror the characters from the N-th position up to the length of the string in alphabetical order. In mirror operation, we change ‘a’ to ‘z’, ‘b’ to ‘y’, and so on.

Examples: 

Input : N = 3
        paradox
Output : paizwlc
We mirror characters from position 3 to end.

Input : N = 6
        pneumonia
Output : pneumlmrz

We have an existing solution for this problem please refer to Mirror characters of a string link. We can solve this problem in Python using Dictionary Data Structure. The mirror value of ‘a’ is ‘z’,’b’ is ‘y’, etc, so we create a dictionary data structure and one-to-one map reverse sequence of alphabets onto the original sequence of alphabets. Now traverse characters from length k in given string and change characters into their mirror value using a dictionary.

Implementation:

Python3




# function to mirror characters of a string
 
def mirrorChars(input,k):
 
    # create dictionary
    original = 'abcdefghijklmnopqrstuvwxyz'
    reverse = 'zyxwvutsrqponmlkjihgfedcba'
    dictChars = dict(zip(original,reverse))
 
    # separate out string after length k to change
    # characters in mirror
    prefix = input[0:k-1]
    suffix = input[k-1:]
    mirror = ''
 
    # change into mirror
    for i in range(0,len(suffix)):
         mirror = mirror + dictChars[suffix[i]]
 
    # concat prefix and mirrored part
    print (prefix+mirror)
          
# Driver program
if __name__ == "__main__":
    input = 'paradox'
    k = 3
    mirrorChars(input,k)


Output

paizwlc

Time complexity: O(n)
Auxiliary Space: O(n)


Similar Reads

Check if a number is prime in Flipped Upside Down, Mirror Flipped and Mirror Flipped Upside Down
Given an integer N, the task is to check if N is a prime number in Flipped Down, Mirror Flipped and Mirror Flipped Down forms of the given number.Examples: Input: N = 120121 Output: YesExplanation: Flipped forms of the number:Flipped Upside Down - 151051Mirror Flipped - 121021Mirror Upside Down - 150151Since 1510151 and 121021 are both prime number
6 min read
Mirror characters of a string
Given a string and a number N, we need to mirror the characters from N-th position up to the length of the string in the alphabetical order. In mirror operation, we change 'a' to 'z', 'b' to 'y', and so on. Examples: Input : N = 3 paradoxOutput : paizwlcWe mirror characters from position 3 to end.Input : N = 6 pneumoniaOutput : pnefnlmrzBelow are d
9 min read
Python - Mirror Image of String
Given a String, perform its mirror imaging, return "Not Possible" if mirror image not possible using english characters. Input : test_str = 'boid' Output : doib Explanation : d replaced by b and vice-versa as being mirror images. Input : test_str = 'gfg' Output : Not Possible Explanation : Valid Mirror image not possible. Method : Using loop + look
3 min read
Convert String Dictionary to Dictionary Python
Interconversions of data types have been discussed many times and have been quite a popular problem to solve. This article discusses yet another problem of interconversion of the dictionary, in string format to a dictionary. Let's discuss certain ways in which this can be done. Convert String Dictionary to Dictionary Using json.loads() This task ca
6 min read
Find largest word in dictionary by deleting some characters of given string
Giving a dictionary and a string 'str', find the longest string in dictionary which can be formed by deleting some characters of the given 'str'. Examples: Input : dict = {"ale", "apple", "monkey", "plea"} str = "abpcplea" Output : apple Input : dict = {"pintu", "geeksfor", "geeksgeeks", " forgeek"} str = "geeksforgeeks" Output : geeksgeeks Asked I
14 min read
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
5 min read
Find mirror of a given node in Binary tree
Given a Binary tree, the problem is to find the mirror of a given node. The mirror of a node is a node which exists at the mirror position of the node in opposite subtree at the root. Examples: In above tree- Node 2 and 3 are mirror nodes Node 4 and 6 are mirror nodes. Recommended PracticeMirror of a given nodeTry It! We can have a recursive soluti
15+ min read
Find mirror image of a point in 2-D plane
Given a point P in 2-D plane and equation of mirror, the task is to find the image of that point Q formed due to the mirror. Equation of mirror is in form ax + by + c = 0Examples: Input : P = (1, 0), a = -1, b = 1, c = 0Output : Q = (0, 1) Input : P = (3, 3), a = 0, b = 1, c = -2Output : Q = (3, 1) Solution : Let coordinate of P(given point) be (x1
7 min read
Modify characters of a string by adding integer values of same-indexed characters from another given string
Given two strings S and N of the same length, consisting of alphabetical and numeric characters respectively, the task is to generate a new string obtained by adding the integer value of each character of string N with the ASCII value of the same indexed character of string S. Finally, print the resultant string.Note: If the sum exceeds 122, then s
6 min read
Longest Common Subsequence (LCS) by repeatedly swapping characters of a string with characters of another string
Given two strings A and B of lengths N and M respectively, the task is to find the length of the longest common subsequence that can be two strings if any character from string A can be swapped with any other character from B any number of times. Examples: Input: A = "abdeff", B = "abbet"Output: 4Explanation: Swapping A[5] and B[4] modifies A to "a
7 min read
three90RightbarBannerImg