Character replacement after removing duplicates from a string
Given a string. The task is to replace each character of the minimized string by a character present at 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 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 : geeks
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 : oeoeeohApproach:
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 stringAlgorithm 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 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 = "geeks"; replaceMinimizeUtil(str); return 0;} |
Java
// Java program for character replacement// after String minimizationclass 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 = "geeks"; replaceMinimizeUtil(str); }}// This code is contributed by Rajput-JI |
Python3
# Python3 program for character# replacement after string minimization# Function to minimize stringdef 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 stringdef 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 programif __name__ == "__main__": string = "geeks" 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 = "geeks"; 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 = "geeks";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 = "geeks";replaceMinimizeUtil(str);// This code is contributed by avanitrachhadiya2155</script> |
Final string: ssesg
Time Complexity: O(n)
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks 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.



