Mirror Characters of a String in Java31 Mar 2025 | 5 min read Mirroring characters of a string replaces each letter with its counterpart from the opposite end of the alphabet (e.g., 'a' becomes 'z', 'b' becomes 'y'). Uppercase and lowercase letters retain their case, and non-alphabet characters remain unchanged. This technique is useful in encoding and text transformations. Example 1: Input: N = 2 hello Output: heovv (Mirroring starts from the 2nd position) Example 2: Input: N = 4 programming Output: progmlmtorn (Mirroring starts from the 4th position) Example 3: Input: N = 5 fantastic Output: fantzghxr (Mirroring starts from the 5th position) Approach: Mathematical Character MappingMathematical Character Mapping involves transforming characters by calculating their mirrored counterparts based on their positions in the alphabet using arithmetic operations. This approach efficiently replaces each character with its opposite by leveraging the ASCII values, allowing for a straightforward implementation without the need for additional data structures. AlgorithmStep 1: A string input containing a mix of uppercase letters, lowercase letters, and non-alphabet characters. Step 2: Create a StringBuilder called mirrored to build the mirrored string efficiently. Step 3: For each character ch: a. Check if ch is a lowercase letter:
Step 4: After determining the mirrored character (or the unchanged non-alphabet character), append it to the mirrored string. Step 5: Once all characters are processed, convert the StringBuilder to a string and return it. ImplementationFile Name: MirrorCharacters.java Output: zyxCBA Time Complexity: O(n), where n is the length of the input string. Auxiliary Space Complexity: O(n) for storing the mirrored result. Approach: Using MappingA HashMap is used to assign each alphabet character to its corresponding mirrored character (e.g., 'a' to 'z', 'b' to 'y'). The program creates a new mirrored string by efficiently manipulating the input string using a StringBuilder to find and replace each character with its mirror in the map. AlgorithmStep 1: Start with an input string (e.g., "abcXYZ"). Step 2: Call the mirrorString() Function with the input string to generate its mirrored version. Step 3: In the mirrorString() function, first call the createMirrorMap() to create a map that defines the mirror relationships for characters. Step 4: Initialize a HashMap<Character, Character> to store mappings of each character to its mirrored counterpart. Step 5: In the mirrorString() function, use a StringBuilder to build the mirrored version of the input string.
ImplementationFile Name: MirrorCharactersUsingMap.java Output: zyxCBA Time Complexity: O(n), where n is the length of the input string. Auxiliary Space Complexity: O(n) for storing the mirrored string. Next TopicJava Spring Pros & Cons |
The Triangle Inequality Theorem is used to check if three given edges can create a triangle. The theorem affirms that the total of two sides needs to exceed the third side. Using this rule, we can quickly verify if the edges can form a valid triangle,...
5 min read
In Java, there are many ways to swap two numbers. Generally, we use either swap() method of the Math class or use a third (temporary) variable to swap two numbers. Except these two ways, we can also swap two numbers using the bitwise operator (XOR) and...
3 min read
The valueOf() method is a static method that returns the relevant Integer Object holding the value of the argument passed. The argument can be a primitive data type, String, etc. Java's valueOf() function is an essential tool for mapping different data types-such as strings and...
6 min read
In this section, we will learn how to interchange diagonals of a matrix through a Java program. It is usually asked in Java interviews and academics. Consider the above 4*4 matrix whose size is n. In the above matrix, we have to swap the following indexes to interchange...
4 min read
java.net Java programs are built specially to be run on a network. For the practice of these network applications, a set of classes is provided under this package. A summary of various classes of is given below: Class Description Authenticator For networking applications it is first important to fetch the...
6 min read
Every local variable and the final blank field will have an assigned value when any value is accessed. Access to the value will consist of the variable's name or an area that occurs in an expression, except in the left-hand operand of the assignment operator, "=". To...
15 min read
The Evil number is another special positive whole number in Java that has an even number of 1's in its binary equivalent. Unlike Prime and Armstrong numbers, Evil number is not so popular and asked by the interviewers. The numbers which are not evil are called odious...
3 min read
Linking classes together is a crucial component of object-oriented programming (OOP), a complex web of interconnectivity. The three basic ideas that underpin these interactions are introduced by Java, a language well-known for its object-oriented principles: association, aggregation, and composition. These ideas not only serve as the...
8 min read
The java.text.CollationElementIterator has a secondaryOrder() method. Every Collation element of a CollationElementIterator object has a secondary component, which is provided by the CollationElementIterator class. Syntax: public static final short secondaryOrder(int order) Parameter: The above method requires the secondary component to be located for a collation element...
4 min read
Java is a popular programming language used in the development of applications across various platforms. When working with dates, it is often necessary to extract specific information such as the day of the week from a given date. In this section, we will discuss how to...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India