📌  相关文章
📜  通过删除和重新排列字符从另一个字符串中创建一个字符串

📅  最后修改于: 2022-05-13 01:57:08.530000             🧑  作者: Mango

通过删除和重新排列字符从另一个字符串中创建一个字符串

给定两个字符串,找出我们是否可以通过从第二个字符中删除一些字符并重新排列剩余字符来从第二个字符串中生成第一个字符串。
例子:

Input : s1 = ABHISHEKsinGH
      : s2 = gfhfBHkooIHnfndSHEKsiAnG
Output : Possible

Input : s1 = Hello
      : s2 = dnaKfhelddf
Output : Not Possible

Input : s1 = GeeksforGeeks
      : s2 = rteksfoGrdsskGeggehes
Output : Possible

我们基本上需要查找一个字符串是否包含作为第二个字符串中字符子集的字符。首先,我们计算第二个字符串中所有字符的出现次数。然后我们遍历第一个字符串并减少第一个出现的每个字符的计数。如果在任何时候,count 小于 0,我们返回 false。如果所有计数都大于或等于 0,则返回 true。

C++
// CPP program to find if it is possible to
// make a string from characters present in
// other string.
#include 
using namespace std;
 
const int MAX_CHAR = 256;
 
// Returns true if it is possible to make
// s1 from characters present in s2.
bool isPossible(string& s1, string& s2)
{
    // Count occurrences of all characters
    // present in s2..
    int count[MAX_CHAR] = { 0 };
    for (int i = 0; i < s2.length(); i++)
        count[s2[i]]++;
 
    // For every character, present in s1,
    // reduce its count if count is more
    // than 0.
    for (int i = 0; i < s1.length(); i++) {
        if (count[s1[i]] == 0)
            return false;
        count[s1[i]]--;
    }
 
    return true;
}
 
// Driver code
int main()
{
    string s1 = "GeeksforGeeks",
           s2 = "rteksfoGrdsskGeggehes";
    if (isPossible(s1, s2))
        cout << "Possible";
    else
        cout << "Not Possible\n";
    return 0;
}


Java
// Java program to find if it is possible to
// make a string from characters present in
// other string.
class StringfromCharacters
{
    static final int MAX_CHAR = 256;
 
    // Returns true if it is possible to make
    // s1 from characters present in s2.
    static boolean isPossible(String s1, String s2)
    {
        // Count occurrences of all characters
        // present in s2..
        int count[] = new int[MAX_CHAR];
        for (int i = 0; i < s2.length(); i++)
            count[(int)s2.charAt(i)]++;
 
        // For every character, present in s1,
        // reduce its count if count is more
        // than 0.
        for (int i = 0; i < s1.length(); i++) {
            if (count[(int)s1.charAt(i)] == 0)
                return false;
            count[(int)s1.charAt(i)]--;
        }
     
        return true;
    }
     
    // Driver code
    public static void main(String args[])
    {
        String s1 = "GeeksforGeeks",
            s2 = "rteksfoGrdsskGeggehes";
        if (isPossible(s1, s2))
            System.out.println("Possible");
        else
            System.out.println("Not Possible");
    }
}
 
/* This code is contributed by Danish Kaleem */


Python 3
# Python 3 program to find if it is possible
# to make a string from characters present
# in other string.
MAX_CHAR = 256
 
# Returns true if it is possible to make
# s1 from characters present in s2.
def isPossible(s1, s2):
 
    # Count occurrences of all characters
    # present in s2..
    count = [0] * MAX_CHAR
    for i in range(len(s2)):
        count[ord(s2[i])] += 1
 
    # For every character, present in s1,
    # reduce its count if count is more
    # than 0.
    for i in range(len(s1)):
        if (count[ord(s1[i])] == 0):
            return False
        count[ord(s1[i])] -= 1
 
    return True
 
# Driver code
if __name__ == "__main__":
     
    s1 = "GeeksforGeeks"
    s2 = "rteksfoGrdsskGeggehes"
    if (isPossible(s1, s2)):
        print("Possible")
    else:
        print("Not Possible")
 
# This code is contributed by ita_c


C#
// C# program to find if it is possible to
// make a string from characters present
// in other string.
using System;
 
class GFG {
     
    static int MAX_CHAR = 256;
 
    // Returns true if it is possible to
    // make s1 from characters present
    // in s2.
    static bool isPossible(String s1, String s2)
    {
         
        // Count occurrences of all characters
        // present in s2..
        int []count = new int[MAX_CHAR];
         
        for (int i = 0; i < s2.Length; i++)
            count[(int)s2[i]]++;
 
        // For every character, present in s1,
        // reduce its count if count is more
        // than 0.
        for (int i = 0; i < s1.Length; i++)
        {
            if (count[(int)s1[i]] == 0)
                return false;
                 
            count[(int)s1[i]]--;
        }
     
        return true;
    }
     
    // Driver code
    public static void Main()
    {
        string s1 = "GeeksforGeeks",
               s2 = "rteksfoGrdsskGeggehes";
                
        if (isPossible(s1, s2))
            Console.WriteLine("Possible");
        else
            Console.WriteLine("Not Possible");
    }
}
 
// This code is contributed by vt_m.


Javascript


输出:

Possible