📌  相关文章
📜  根据给定的条件从给定的字符串P和Q生成一个字符串

📅  最后修改于: 2021-04-29 02:12:53             🧑  作者: Mango

给定两个字符串PQ,任务是生成满足以下条件的字符串S

  • 查找S ,以使P重新排列,而Q是其中的子字符串。
  • S中Q之前的所有字符均应小于或等于Q中按字母顺序排列的第一个字符。
  • 其余字符应按字符顺序排列在Q之后

注意: Q的所有字符始终存在于P中,并且Q的长度始终小于或等于P的长度。

例子:

方法:想法是找到 P和Q中所有字符的频率可以解决该问题。

  • 维持P和Q中所有字母的频率数组。
  • 找到频率后,根据在Q中的第一个字符P中分离字符,并将它们添加到所得到的字符串。
  • 最后返回结果字符串。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to generate a string S from string P
// and Q according to the given conditions
void manipulateStrings(string P, string Q)
{
 
    // Stores the frequencies
    int freq[26];
    memset(freq, 0, sizeof(freq));
     
    // Counts occurrences of each characters
    for(int i = 0; i < P.size(); i++)
    {
        freq[P[i] - 'a']++;
    }
 
    // Reduce the count of the character
    // which is also present in Q
    for(int i = 0; i < Q.size(); i++)
    {
        freq[Q[i] - 'a']--;
    }
 
    // Stores the resultant string
    string sb = "";
 
    // Index in freq[] to segregate the string
    int pos = Q[0] - 'a';
 
    for(int i = 0; i <= pos; i++)
    {
        while (freq[i] > 0)
        {
            char c = (char)('a' + i);
            sb += c;
            freq[i]--;
        }
    }
 
    // Add Q to the resulting string
    sb += Q;
 
    for(int i = pos + 1; i < 26; i++)
    {
        while (freq[i] > 0)
        {
            char c = (char)('a' + i);
            sb += c;
            freq[i]--;
        }
    }
 
    cout << sb << endl;
}
 
// Driver Code
int main()
{
    string P = "geeksforgeeksfor";
    string Q = "for";
     
    // Function call
    manipulateStrings(P, Q);
}
 
// This code is contributed by Amit Katiyar


Java
// Java Program to implement
// the above approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
 
    // Function to generate a string S from string P
    // and Q according to the given conditions
    public static void manipulateStrings(String P,
                                         String Q)
    {
 
        // Stores the frequencies
        int[] freq = new int[26];
 
        // Counts occurrences of each characters
        for (int i = 0; i < P.length(); i++) {
            freq[P.charAt(i) - 'a']++;
        }
 
        // Reduce the count of the character
        // which is also present in Q
        for (int i = 0; i < Q.length(); i++) {
            freq[Q.charAt(i) - 'a']--;
        }
 
        // Stores the resultant string
        StringBuilder sb
            = new StringBuilder();
 
        // Index in freq[] to segregate the string
        int pos = Q.charAt(0) - 'a';
 
        for (int i = 0; i <= pos; i++) {
            while (freq[i] > 0) {
                char c = (char)('a' + i);
                sb.append(c);
                freq[i]--;
            }
        }
 
        // Add Q to the resulting string
        sb.append(Q);
 
        for (int i = pos + 1; i < 26; i++) {
            while (freq[i] > 0) {
                char c = (char)('a' + i);
                sb.append(c);
                freq[i]--;
            }
        }
 
        System.out.println(sb);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String P = "geeksforgeeksfor";
        String Q = "for";
        // Function call
        manipulateStrings(P, Q);
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to generate a string S
# from string P and Q according to
# the given conditions
def manipulateStrings(P, Q):
  
    # Stores the frequencies
    freq = [0 for i in range(26)];
      
    # Counts occurrences of each characters
    for i in range(len(P)):
        freq[ord(P[i]) - ord('a')] += 1
  
    # Reduce the count of the character
    # which is also present in Q
    for i in range(len(Q)):
        freq[ord(Q[i]) - ord('a')] -= 1
  
    # Stores the resultant string
    sb = ""
  
    # Index in freq[] to segregate the string
    pos = ord(Q[0]) - ord('a')
     
    for i in range(pos + 1):
        while freq[i] > 0:
            sb += chr(ord('a') + i)
            freq[i] -= 1
  
    # Add Q to the resulting string
    sb += Q
     
    for i in range(pos + 1, 26):
        while freq[i] > 0:
            sb += chr(ord('a') + i)
            freq[i] -= 1
     
    print(sb)
     
# Driver Code
if __name__=="__main__":
     
    P = "geeksforgeeksfor";
    Q = "for";
      
    # Function call
    manipulateStrings(P, Q);
 
# This code is contributed by rutvik_56


C#
// C# Program to implement
// the above approach
using System;
class GFG{
 
  // Function to generate a string S from string P
  // and Q according to the given conditions
  public static void manipulateStrings(String P,
                                       String Q)
  {
 
    // Stores the frequencies
    int[] freq = new int[26];
 
    // Counts occurrences of each characters
    for (int i = 0; i < P.Length; i++)
    {
      freq[P[i] - 'a']++;
    }
 
    // Reduce the count of the character
    // which is also present in Q
    for (int i = 0; i < Q.Length; i++)
    {
      freq[Q[i] - 'a']--;
    }
 
    // Stores the resultant string
    String sb = "";
 
    // Index in []freq to segregate the string
    int pos = Q[0] - 'a';
 
    for (int i = 0; i <= pos; i++)
    {
      while (freq[i] > 0)
      {
        char c = (char)('a' + i);
        sb += c;
        freq[i]--;
      }
    }
 
    // Add Q to the resulting string
    sb += Q;
 
    for (int i = pos + 1; i < 26; i++)
    {
      while (freq[i] > 0)
      {
        char c = (char)('a' + i);
        sb += c;
        freq[i]--;
      }
    }
    Console.WriteLine(sb);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    String P = "geeksforgeeksfor";
    String Q = "for";
     
    // Function call
    manipulateStrings(P, Q);
  }
}
 
// This code is contributed by Rohit_ranjan


输出:
eeeefforggkkorss

时间复杂度: O(N + M),其中N和M分别是P和Q的长度。
辅助空间: O(1)