Open In App

Find an anagram of given String having different characters at corresponding indices

Last Updated : 01 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S consisting of N characters, the task is to find the anagram of the given string S such that the characters at the same indices are different from the original string.

Examples:

Input: S = “geek”
Output: egke
Explanation:
The anagram of the given string such that all the characters at all the corresponding indices are not same is “egke”.  

Input: S = “aaaa”
Output: -1

Approach: The given problem can be solved by using two pointers approach. The idea is to swap the different characters at the end of the string and if the characters at those indices are the same then check for the next possible pairs of indices having different characters. After performing the above operations check if the anagram generated different characters at every index or not and print the result accordingly. Follow the steps below to solve the given problem:

  • Initialize a string, say T that stores the given string S.
  • Initialize two pointers, say and j as 0 and (N – 1) respectively.
  • Iterate until the value of i is less than N and j is non-negative and perform the following steps:
    1. If the characters S[i] and S[j] are not the same and the pairs of characters (S[i], T[j]) and (T[i], S[j]) are also not the same(that ensure the characters after swapping operation becomes the same as the original), then swap the characters (S[i], S[j]) and update the value of j to (N – 1).
    2. Otherwise, increment the value of i by 1.
  • If the string has an odd number of characters and the characters at middle indices are equal then perform the swap operation as illustrated in the above steps.
  • After completing the above steps, if characters at the corresponding indices of the string S and T are not the same, then print the string S as the resultant string. Otherwise, print “-1”.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find anagram of string
// such that characters at the same
// indices are different
void findAnagram(string s)
{
    // Copying our original string
    // for comparison
    string check = s;
 
    // Declaring the two pointers
    int i = 0, j = s.length() - 1;
 
    while (i < s.length() && j >= 0) {
 
        // Checking the given condition
        if (s[i] != s[j] && check[i] != s[j]
            && check[j] != s[i]) {
            swap(s[i], s[j]);
            i++;
 
            j = s.length() - 1;
        }
        else {
            j--;
        }
    }
 
    // When string length is odd
    if (s.length() % 2 != 0) {
 
        // The mid element
        int mid = s.length() / 2;
 
        // If the characters are the
        // same, then perform the swap
        // operation as illustrated
        if (check[mid] == s[mid]) {
            for (int i = 0; i < s.length(); i++) {
                if (check[i] != s[mid]
                    && s[i] != s[mid]) {
                    swap(s[i], s[mid]);
                    break;
                }
            }
        }
    }
 
    // Check if the corresponding indices
    // has the same character or not
    bool ok = true;
    for (int i = 0; i < s.length(); i++) {
        if (check[i] == s[i]) {
            ok = false;
            break;
        }
    }
 
    // If string follows required
    // condition
    if (ok)
        cout << s;
    else
        cout << -1;
}
 
// Driver Code
int main()
{
    string S = "geek";
    findAnagram(S);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG {
 
// Function to find anagram of string
// such that characters at the same
// indices are different
static void findAnagram(String s)
{
    
    // Copying our original string
    // for comparison
    String check = s;
  
    // Declaring the two pointers
    int i = 0, j = s.length() - 1;
  
    while (i < s.length() && j >= 0) {
  
        // Checking the given condition
        if (s.charAt(i) != s.charAt(j) && check.charAt(i) != s.charAt(j)
            && check.charAt(j) != s.charAt(i)) {
            char temp = s.charAt(i);
            s = s.substring(0, i) + s.charAt(j) + s.substring(i + 1);
            s = s.substring(0, j) + temp + s.substring(j + 1);
            i++;
  
            j = s.length() - 1;
        }
        else {
            j--;
        }
    }
  
    // When string length is odd
    if (s.length() % 2 != 0) {
  
        // The mid element
        int mid = s.length() / 2;
  
        // If the characters are the
        // same, then perform the swap
        // operation as illustrated
        if (check.charAt(mid) == s.charAt(mid)) {
            for (i = 0; i < s.length(); i++) {
                if (check.charAt(i) != s.charAt(mid)
                    && s.charAt(i) != s.charAt(mid)) {
                    char temp = s.charAt(i);
                    s = s.substring(0, i) + s.charAt(mid) + s.substring(i + 1);
                    s = s.substring(0, mid) + temp + s.substring(mid + 1);
                    break;
                }
            }
        }
    }
  
    // Check if the corresponding indices
    // has the same character or not
    boolean ok = true;
     
    for (i = 0; i < s.length(); i++) {
        if (check.charAt(i) == s.charAt(i) ) {
            ok = false;
            break;
        }
    }
  
    // If string follows required
    // condition
    if (ok)
        System.out.println(s);
    else
        System.out.println(-1);
}
 
// Driver Code
public static void main (String[] args)
{
    String S = "geek";
    findAnagram(S);
}
}
 
// This code is contributed by sanjoy_62.


Python3




# Python 3 program for the above approach
 
# Function to find anagram of string
# such that characters at the same
# indices are different
def findAnagram(s):
 
    # Copying our original string
    # for comparison
    check = s
    st = list(s)
 
    # Declaring the two pointers
    i = 0
    j = len(st) - 1
 
    while (i < len(st) and j >= 0):
 
        # Checking the given condition
        if (st[i] != st[j] and check[i] != st[j]
                and check[j] != st[i]):
            st[i], st[j] = st[j], st[i]
            i += 1
 
            j = len(st) - 1
 
        else:
            j -= 1
 
    # When string length is odd
    if (len(st) % 2 != 0):
 
        # The mid element
        mid = len(st) / 2
 
        # If the characters are the
        # same, then perform the swap
        # operation as illustrated
        if (check[mid] == st[mid]):
            for i in range(len(st)):
                if (check[i] != st[mid]
                        and st[i] != st[mid]):
                    st[i], st[mid] = st[mid], st[i]
                    break
 
    # Check if the corresponding indices
    # has the same character or not
    ok = True
    for i in range(len(st)):
        if (check[i] == st[i]):
            ok = False
            break
 
    # If string follows required
    # condition
    if (ok):
        print("".join(st))
    else:
        print(-1)
 
# Driver Code
if __name__ == "__main__":
 
    S = "geek"
    findAnagram(S)
 
    # This code is contributed by ukasp.


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find anagram of string
// such that characters at the same
// indices are different
static void findAnagram(string s)
{
   
    // Copying our original string
    // for comparison
    string check = s;
 
    // Declaring the two pointers
    int i = 0, j = s.Length - 1;
 
    while (i < s.Length && j >= 0) {
 
        // Checking the given condition
        if (s[i] != s[j] && check[i] != s[j]
            && check[j] != s[i]) {
            char temp = s[i];
            s = s.Substring(0, i) + s[j] + s.Substring(i + 1);
            s = s.Substring(0, j) + temp + s.Substring(j + 1);
            i++;
 
            j = s.Length - 1;
        }
        else {
            j--;
        }
    }
 
    // When string length is odd
    if (s.Length % 2 != 0) {
 
        // The mid element
        int mid = s.Length / 2;
 
        // If the characters are the
        // same, then perform the swap
        // operation as illustrated
        if (check[mid] == s[mid]) {
            for (i = 0; i < s.Length; i++) {
                if (check[i] != s[mid]
                    && s[i] != s[mid]) {
                    char temp = s[i];
                    s = s.Substring(0, i) + s[mid] + s.Substring(i + 1);
                    s = s.Substring(0, mid) + temp + s.Substring(mid + 1);
                    break;
                }
            }
        }
    }
 
    // Check if the corresponding indices
    // has the same character or not
    bool ok = true;
    for (i = 0; i < s.Length; i++) {
        if (check[i] == s[i]) {
            ok = false;
            break;
        }
    }
 
    // If string follows required
    // condition
    if (ok)
        Console.Write(s);
    else
        Console.Write(-1);
}
 
// Driver Code
public static void Main()
{
    string S = "geek";
    findAnagram(S);
}
}
 
// This code is contributed by ipg2016107.


Javascript




<script>
        // JavaScript Program to implement
        // the above approach
 
        // Function to find anagram of string
        // such that characters at the same
        // indices are different
        function swap(str, i, j) {
            if (i == j) {
                return str;
            }
 
            if (j < i) {
                var temp = j;
                j = i;
                i = temp;
            }
 
            if (i >= str.length) {
                return str;
 
            }
            return str.substring(0, i) +
                str[j] +
 
                str.substring(i + 1, j) +
 
                str[i] +
                str.substring(j + 1);
        }
 
        function findAnagram(s)
        {
         
            // Copying our original string
            // for comparison
            let check = s.slice();
 
            // Declaring the two pointers
            let i = 0, j = s.length - 1;
 
            while (i < s.length && j >= 0) {
 
                // Checking the given condition
                if (s[i] != s[j] && check[i] != s[j]
                    && check[j] != s[i]) {
                    s = swap(s, i, j)
                    i++;
 
                    j = s.length - 1;
                }
                else {
                    j--;
                }
            }
 
            // When string length is odd
            if (s.length % 2 != 0) {
 
                // The mid element
                let mid = s.length / 2;
 
                // If the characters are the
                // same, then perform the swap
                // operation as illustrated
                if (check[mid] == s[mid]) {
                    for (let i = 0; i < s.length; i++) {
                        if (check[i] != s[mid]
                            && s[i] != s[mid]) {
                            s = swap(s, i, mid)
                            break;
                        }
                    }
                }
            }
             
            // Check if the corresponding indices
            // has the same character or not
            let ok = true;
            for (let i = 0; i < s.length; i++) {
                if (check[i] == s[i]) {
                    ok = false;
                    break;
                }
            }
 
            // If string follows required
            // condition
            if (ok)
                document.write(s);
            else
                document.write(-1);
        }
 
        // Driver Code
 
        let S = "geek";
        findAnagram(S);
 
// This code is contributed by Potta Lokesh
    </script>


Output: 

egke

 

Time Complexity: O(N*log N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads