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 : 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 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 = "geeks" ; 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 = "geeks" ; 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 = "geeks" 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 = "geeks" ; 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 = "geeks" ; replaceMinimizeUtil( $str ); // This code is contributed by mits ?> |
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.