Subsequences generated by including characters or ASCII value of characters of given string
Given a string str of length N, the task is to print all possible non-empty subsequences of the given string such that the subsequences either contain characters or ASCII values of the characters from the given string.
Examples:
Input: str = “ab”
Output: b 98 a ab a98 97 97b 9798
Explanation:
Possible subsequence of the strings are { b, a, ab }.
Possible subsequences of the string generated by including either the characters or the ASCII value of the characters from the given string are { 98, b, a, 97, ab, 97b, a98, 9798 }.
Therefore, the required output is { b, 98, a, ab, a98, 97, 97b, 9798 }.
Input: str = “a”
Output: a 97
Approach: Follow the steps below to solve the problem:
FindSub(str, res, i) = { FindSub(str, res, i + 1), FindSub(str, res + str[i], i + 1), FindSub(str, res + ASCII(str[i]), i + 1) }
res = subsequence of the string
i = index of a character in str
- Using the above recurrence relation, print all possible subsequences based on the given conditions.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void FindSub(string str, string res,
int i)
{
if (i == str.length())
{
if (res.length() > 0)
{
cout << res << " " ;
}
return ;
}
char ch = str[i];
FindSub(str, res, i + 1);
FindSub(str, res + ch, i + 1);
FindSub(str, res + to_string( int (ch)), i + 1);
}
int main()
{
string str = "ab" ;
string res = "" ;
int N = str.length();
FindSub(str, res, 0);
}
|
Java
class GFG {
static void FindSub(String str, String res,
int i)
{
if (i == str.length()) {
if (res.length() > 0 ) {
System.out.print(res + " " );
}
return ;
}
char ch = str.charAt(i);
FindSub(str, res, i + 1 );
FindSub(str, res + ch, i + 1 );
FindSub(str, res + ( int )ch, i + 1 );
;
}
public static void main(String[] args)
{
String str = "ab" ;
String res = "" ;
int N = str.length();
FindSub(str, res, 0 );
}
}
|
Python3
def FindSub(string , res, i) :
if (i = = len (string)):
if ( len (res) > 0 ) :
print (res, end = " " );
return ;
ch = string[i];
FindSub(string, res, i + 1 );
FindSub(string, res + ch, i + 1 );
FindSub(string, res + str ( ord (ch)), i + 1 );
if __name__ = = "__main__" :
string = "ab" ;
res = "";
N = len (string);
FindSub(string, res, 0 );
|
C#
using System;
class GFG{
static void FindSub( string str, string res,
int i)
{
if (i == str.Length)
{
if (res.Length > 0)
{
Console.Write(res + " " );
}
return ;
}
char ch = str[i];
FindSub(str, res, i + 1);
FindSub(str, res + ch, i + 1);
FindSub(str, res + ( int )ch, i + 1);
}
public static void Main(String[] args)
{
string str = "ab" ;
string res = "" ;
int N = str.Length;
FindSub(str, res, 0);
}
}
|
Javascript
<script>
function FindSub(str, res, i)
{
if (i === str.length)
{
if (res.length > 0)
{
document.write(res + " " );
}
return ;
}
var ch = str[i];
FindSub(str, res, i + 1);
FindSub(str, res + ch, i + 1);
FindSub(str, res + ch.charCodeAt(0), i + 1);
}
var str = "ab" ;
var res = "" ;
var N = str.length;
FindSub(str, res, 0);
</script>
|
Output
b 98 a ab a98 97 97b 9798
Time Complexity: O(3N)
Auxiliary Space: O(N)
Last Updated :
08 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...