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

📅  最后修改于: 2021-10-27 08:27: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


Javascript


输出:
eeeefforggkkorss

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

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