Make a string from another by deletion and rearrangement of characters
Last Updated :
21 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 256;
bool isPossible(string& s1, string& s2)
{
int count[MAX_CHAR] = { 0 };
for ( int i = 0; i < s2.length(); i++)
count[s2[i]]++;
for ( int i = 0; i < s1.length(); i++) {
if (count[s1[i]] == 0)
return false ;
count[s1[i]]--;
}
return true ;
}
int main()
{
string s1 = "GeeksforGeeks" ,
s2 = "rteksfoGrdsskGeggehes" ;
if (isPossible(s1, s2))
cout << "Possible" ;
else
cout << "Not Possible\n" ;
return 0;
}
|
Java
class StringfromCharacters
{
static final int MAX_CHAR = 256 ;
static boolean isPossible(String s1, String s2)
{
int count[] = new int [MAX_CHAR];
for ( int i = 0 ; i < s2.length(); i++)
count[( int )s2.charAt(i)]++;
for ( int i = 0 ; i < s1.length(); i++) {
if (count[( int )s1.charAt(i)] == 0 )
return false ;
count[( int )s1.charAt(i)]--;
}
return true ;
}
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" );
}
}
|
Python 3
MAX_CHAR = 256
def isPossible(s1, s2):
count = [ 0 ] * MAX_CHAR
for i in range ( len (s2)):
count[ ord (s2[i])] + = 1
for i in range ( len (s1)):
if (count[ ord (s1[i])] = = 0 ):
return False
count[ ord (s1[i])] - = 1
return True
if __name__ = = "__main__" :
s1 = "GeeksforGeeks"
s2 = "rteksfoGrdsskGeggehes"
if (isPossible(s1, s2)):
print ( "Possible" )
else :
print ( "Not Possible" )
|
C#
using System;
class GFG {
static int MAX_CHAR = 256;
static bool isPossible(String s1, String s2)
{
int []count = new int [MAX_CHAR];
for ( int i = 0; i < s2.Length; i++)
count[( int )s2[i]]++;
for ( int i = 0; i < s1.Length; i++)
{
if (count[( int )s1[i]] == 0)
return false ;
count[( int )s1[i]]--;
}
return true ;
}
public static void Main()
{
string s1 = "GeeksforGeeks" ,
s2 = "rteksfoGrdsskGeggehes" ;
if (isPossible(s1, s2))
Console.WriteLine( "Possible" );
else
Console.WriteLine( "Not Possible" );
}
}
|
Javascript
<script>
let MAX_CHAR = 256;
function isPossible(s1, 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 (let i = 0; i < s1.length; i++) {
if (count[s1[i].charCodeAt(0)] == 0)
return false ;
count[s1[i].charCodeAt(0)]--;
}
return true ;
}
let s1 = "GeeksforGeeks" ,
s2 = "rteksfoGrdsskGeggehes" ;
if (isPossible(s1, s2))
document.write( "Possible" );
else
document.write( "Not Possible" );
</script>
|
Time complexity is: O(n)
Auxiliary Space: O(256)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...