Open In App

Make a string from another by deletion and rearrangement of characters

Last Updated : 21 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given two strings, find if we can make first string from second by deleting some characters from second and rearranging remaining characters.

Examples: 

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

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

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

We basically need to find if one string contains characters which are subset of characters in second string. First we count occurrences of all characters in second string. Then we traverse through first string and reduce count of every character that is present in first. If at any moment, count becomes less than 0, we return false. If all counts remain greater than or equal to 0, we return true. 

Implementation:

C++




// CPP program to find if it is possible to
// make a string from characters present in
// other string.
#include <bits/stdc++.h>
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




<script>
// Javascript program to find if it is possible to
// make a string from characters present in
// other string.
     
    let MAX_CHAR = 256;
     
    // Returns true if it is possible to make
    // s1 from characters present in s2.
    function isPossible(s1, s2)
    {
        // Count occurrences of all characters
        // present in s2..
        let count = new Array(MAX_CHAR);
        for(let i = 0; i < count.length; i++)
        {
            count[i] = 0;
        }
        for (let i = 0; i < s2.length; i++)
            count[s2[i].charCodeAt(0)]++;
   
        // For every character, present in s1,
        // reduce its count if count is more
        // than 0.
        for (let i = 0; i < s1.length; i++) {
            if (count[s1[i].charCodeAt(0)] == 0)
                return false;
            count[s1[i].charCodeAt(0)]--;
        }
       
        return true;
    }
     
    // Driver code
    let s1 = "GeeksforGeeks",
            s2 = "rteksfoGrdsskGeggehes";
    if (isPossible(s1, s2))
        document.write("Possible");
    else
        document.write("Not Possible");
         
    // This code is contributed by avanitrachhadiya2155
</script>


Output

Possible

Time complexity is: O(n)
Auxiliary Space: O(256)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads