📌  相关文章
📜  从字符串删除重复项后的字符替换

📅  最后修改于: 2021-10-27 07:55:12             🧑  作者: Mango

给定一个字符串。任务是用原始字符串的索引“ IND ”处的字符替换最小化字符串的每个字符。最小化的字符串是通过从原始字符串保持相同元素的顺序除去所有重复中获得的字符串。

最小化字符串任何索引的 IND 计算如下:

例子:

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

方法:
以下是字符串最小化的分步算法:

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

字符替换算法:

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

下面是上述方法的实现:

C++
// C++ program for character replacement
// after string minimization
#include 
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


Javascript


输出:
Final string: ssesg

时间复杂度:O(n)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程