Monday, October 20, 2025
HomeData Modelling & AICharacter replacement after removing duplicates from a string

Character replacement after removing duplicates from a string

Given a string. The task is to replace each character of the minimized string with a character present at the index ‘IND‘ of the original string. The minimized string is the string obtained by removing all duplicates from the original string keeping the order of elements the same.

IND for any index in the minimized string is calculated as: 

IND = (square of ascii value of minimized string character) % (length of original string)

Examples: 

Input : neveropen
Output : sesg
Explanation :   minimized string = geks
                length of original string = 5         
                ascii value of g = 103
                IND for g = (103*103) % 5 = 4
                replacement character for g = s 
                character 's' present at index 4 of original string
Similarly,
replacement character for e = e 
replacement character for k = s 
replacement character for s = g 

Input : helloworld
Output : oeoeeoh

Approach:
Below is the step-by-step algorithm for string minimization:  

    1. Initialize flagchar[26] = {0}
    2. for i=0 to str.length()-1
    3.     ch = str[i]
    4.     if flagchar[ch-97] == 0 then
    5.         mstr = mstr + ch
    6.         flagchar[ch-97] = 1
    7.     End if
    8. End of loop
    9. return mstr  // minimized string

Algorithm for character replacement: 

    1. Replace each character of minimized string as described
       in the problem statement and example
    2. Compute final string 

Below is the implementation of the above approach: 

C++




// C++ program for character replacement
// after string minimization
#include <bits/stdc++.h>
using namespace std;
 
// Function to minimize string
string minimize(string str)
{
    string mstr = " ";
    int l, i, flagchar[26] = { 0 };
    char ch;
 
    l = str.length();
 
    // duplicate characters are removed
    for (i = 0; i < str.length(); i++) {
        ch = str.at(i);
 
        // checks if character has previously occurred or not
        // if not then add it to the minimized string 'mstr'
        if (flagchar[ch - 97] == 0) {
            mstr = mstr + ch;
            flagchar[ch - 97] = 1;
        }
    }
 
    return mstr; // minimized string
}
 
// Utility function to print the
// minimized, replaced string
void replaceMinimizeUtil(string str)
{
    string minimizedStr, finalStr = "";
    int i, index, l;
    char ch;
    l = str.length();
 
    minimizedStr = minimize(str); // minimized string
 
    // Creating final string by replacing character
    for (i = 0; i < minimizedStr.length(); i++) {
        ch = minimizedStr.at(i);
 
        // index calculation
        index = (ch * ch) % l;
 
        finalStr = finalStr + str.at(index);
    }
 
    cout << "Final string: " << finalStr; // final string
}
 
// Driver program
int main()
{
    string str = "neveropen";
 
    replaceMinimizeUtil(str);
 
    return 0;
}


Java




// Java program for character replacement
// after String minimization
 
class GFG {
    // Function to minimize String
 
    static String minimize(String str)
    {
        String mstr = " ";
        int l, i, flagchar[] = new int[26];
        char ch;
 
        l = str.length();
 
        // duplicate characters are removed
        for (i = 0; i < str.length(); i++) {
            ch = str.charAt(i);
 
            // checks if character has previously occurred or not
            // if not then add it to the minimized String 'mstr'
            if (flagchar[ch - 97] == 0) {
                mstr = mstr + ch;
                flagchar[ch - 97] = 1;
            }
        }
 
        return mstr; // minimized String
    }
 
    // Utility function to print the
    // minimized, replaced String
    static void replaceMinimizeUtil(String str)
    {
        String minimizedStr, finalStr = "";
        int i, index, l;
        char ch;
        l = str.length();
 
        minimizedStr = minimize(str); // minimized String
 
        // Creating final String by replacing character
        for (i = 0; i < minimizedStr.length(); i++) {
            ch = minimizedStr.charAt(i);
 
            // index calculation
            index = (ch * ch) % l;
 
            finalStr = finalStr + str.charAt(index);
        }
        // final String
        System.out.println("Final String: " + finalStr);
    }
 
    // Driver program
    public static void main(String[] args)
    {
        String str = "neveropen";
 
        replaceMinimizeUtil(str);
    }
}
// This code is contributed by Rajput-JI


Python3




# Python3 program for character
# replacement after string minimization
 
# Function to minimize string
def minimize(string):
  
    mstr = " "
    flagchar = [0] * 26 
    l = len(string)
     
    # Duplicate characters are removed
    for i in range(0, len(string)):
      
        ch = string[i]
         
        # checks if character has previously occurred or not
        # if not then add it to the minimized string 'mstr'
        if flagchar[ord(ch)-97] == 0:
          
            mstr = mstr + ch
            flagchar[ord(ch)-97] = 1
                  
    return mstr # minimized string
  
# Utility function to print the
# minimized, replaced string
def replaceMinimizeUtil(string):
  
    finalStr = ""
    l = len(string)
    minimizedStr = minimize(string) # minimized string
     
    # Creating final string by replacing character
    for i in range(0, len(minimizedStr)):
      
        ch = ord(minimizedStr[i])
         
        # index calculation
        index = (ch * ch) % l
        finalStr = finalStr + string[index]
     
    print("Final string:", finalStr) # final string
  
# Driver program
if __name__ == "__main__":
  
    string = "neveropen"
    replaceMinimizeUtil(string)
 
# This code is contributed by Rituraj Jain


C#




// C# program for character replacement
// after String minimization
using System;
 
class GFG {
 
    // Function to minimize String
    static String minimize(string str)
    {
        string mstr = " ";
        int l, i;
        int[] flagchar = new int[26];
        char ch;
 
        l = str.Length;
 
        // duplicate characters are removed
        for (i = 0; i < str.Length; i++) {
            ch = str[i];
 
            // checks if character has previously
            // occurred or not if not then add it
            // to the minimized String 'mstr'
            if (flagchar[ch - 97] == 0) {
                mstr = mstr + ch;
                flagchar[ch - 97] = 1;
            }
        }
 
        return mstr; // minimized String
    }
 
    // Utility function to print the
    // minimized, replaced String
    static void replaceMinimizeUtil(string str)
    {
        string minimizedStr, finalStr = "";
        int i, index, l;
        char ch;
        l = str.Length;
 
        minimizedStr = minimize(str); // minimized String
 
        // Creating final String by
        // replacing character
        for (i = 0; i < minimizedStr.Length; i++) {
            ch = minimizedStr[i];
 
            // index calculation
            index = (ch * ch) % l;
 
            finalStr = finalStr + str[index];
        }
 
        // final String
        Console.Write("Final String: " + finalStr);
    }
 
    // Driver Code
    public static void Main()
    {
        string str = "neveropen";
 
        replaceMinimizeUtil(str);
    }
}
 
// This code is contributed
// by ChitraNayal


PHP




<?php
// PHP program for character replacement
// after string minimization
 
// Function to minimize string
function minimize($str)
{
    $mstr = " ";
    $flagchar=array_fill(0, 26, 0);
 
    $l = strlen($str);
 
    // duplicate characters are removed
    for ($i = 0; $i < strlen($str); $i++)
    {
        $ch = $str[$i];
 
        // checks if character has previously occurred or not
        // if not then add it to the minimized string 'mstr'
        if ($flagchar[ord($ch) - 97] == 0)
        {
            $mstr .=$ch;
            $flagchar[ord($ch) - 97] = 1;
        }
    }
 
    return $mstr; // minimized string
}
 
// Utility function to print the
// minimized, replaced string
function replaceMinimizeUtil($str)
{
 
    $finalStr = "";
 
    $l = strlen($str);
 
    $minimizedStr = minimize($str); // minimized string
 
    // Creating final string by replacing character
    for ($i = 0; $i < strlen($minimizedStr); $i++)
    {
        $ch = $minimizedStr[$i];
 
        // index calculation
        $index = (ord($ch) * ord($ch)) % $l;
 
        $finalStr = $finalStr.$str[$index];
    }
 
    echo "Final string: ".$finalStr; // final string
}
 
// Driver code
$str = "neveropen";
 
replaceMinimizeUtil($str);
 
 
// This code is contributed by mits
?>


Javascript




<script>
 
// Javascript program for character replacement
// after String minimization
 
// Function to minimize String
function minimize(str)
{
    let mstr = " ";
    let l, i, flagchar = new Array(26);
     
    for(let i = 0; i < 26; i++)
    {
        flagchar[i] = 0;
    }
    let ch;
 
    l = str.length;
 
    // Duplicate characters are removed
    for(i = 0; i < str.length; i++)
    {
        ch = str[i];
         
        // Checks if character has previously
        // occurred or not if not then add it
        // to the minimized String 'mstr'
        if (flagchar[ch.charCodeAt(0) - 97] == 0)
        {
            mstr = mstr + ch;
            flagchar[ch.charCodeAt(0) - 97] = 1;
        }
    }
     
    // Minimized String
    return mstr;
}
 
// Utility function to print the
// minimized, replaced String
function replaceMinimizeUtil(str)
{
    let minimizedStr, finalStr = "";
    let i, index, l;
    let ch;
    l = str.length;
     
    // Minimized String
    minimizedStr = minimize(str);
 
    // Creating final String by replacing character
    for(i = 0; i < minimizedStr.length; i++)
    {
        ch = minimizedStr[i].charCodeAt(0);
 
        // Index calculation
        index = (ch * ch) % l;
 
        finalStr = finalStr + str[index];
    }
     
    // Final String
    document.write("Final String: " + finalStr);
}
 
// Driver code
let str = "neveropen";
 
replaceMinimizeUtil(str);
 
// This code is contributed by avanitrachhadiya2155
 
</script>


Output

Final string: ssesg

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n)

This article is contributed by Ayush Jauhari. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS