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 : zambiatek
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 stringstring 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 stringvoid 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 programint main(){Β Β Β Β string str = "zambiatek";Β
Β Β Β Β 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 = "zambiatek";Β
Β Β Β Β Β Β Β Β 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 = "zambiatek"Β Β Β Β replaceMinimizeUtil(string) Β
# This code is contributed by Rituraj Jain |
C#
// C# program for character replacement// after String minimizationusing 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 = "zambiatek";Β
Β Β Β Β Β Β Β Β replaceMinimizeUtil(str);Β Β Β Β }}Β
// This code is contributed// by ChitraNayal |
PHP
<?php// PHP program for character replacement// after string minimizationΒ
// Function to minimize stringfunction 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 stringfunction 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 = "zambiatek";Β
replaceMinimizeUtil($str);Β
Β
// This code is contributed by mits?> |
Javascript
<script>Β
// Javascript program for character replacement// after String minimizationΒ
// Function to minimize Stringfunction 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 Stringfunction 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 codelet str = "zambiatek";Β
replaceMinimizeUtil(str);Β
// This code is contributed by avanitrachhadiya2155Β
</script> |
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 zambiatek and would like to contribute, you can also write an article using write.zambiatek.com or mail your article to review-team@zambiatek.com. See your article appearing on the zambiatek 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.
Β
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



