Make a string from another by deletion and rearrangement of characters

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.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 */

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right



Output:

Possible

Time complexity is: O(n)

This article is contributed by Prabhat kumar singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vt_m, Ita_c



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.