If you ask a coding prodigy about data structures and algorithms, they will tell you how interesting it is!
Indeed, its concepts will intrigue you because they’re extremely useful for our day-to-day lives.
Speaking of everyday life, let’s take an example of a banner that reads, “Save Money.” We usually understand it as a sentence, but if you’re a coder, you will call it a string.
Strings are one of DSA’s prominent concepts that denote continuous characters placed together.
In interviews, many questions related to strings will be asked, such as the subsequences of a string.
If you’re preparing for an interview and you’re wondering about this particular problem, we will give you a thorough guide with examples and solutions. This will help you learn about strings as a whole too!
Get ahead.
What is a string?
In DSA, a string denotes a sequence of characters understood using a computer script. This implies that the string’s characters are associated with a variable, and it helps the computer identify the string.
For eg:
A string variable can be $, and the string can be Hello. So the string attached to a variable would be
$HELLO
With the help of the variable, we can identify that the word HELLO is a string.
Strings have certain library functions required to execute the following purposes:
- Strcat: Assists in combining two strings.
- Strncat: Assists in combining N components of a string into another.
- Strlen: This method returns the length of a string.
- Strcpy: It helps copy a string from one location to another.
- Strncpy: This function duplicates N characters.
- Strcmp: Combines two strings and returns their values.
- Strncmp: The same as strcmp, but compares N characters at first.
- Memset: Begin a string with all nulls.
Did you know strings have so many benefits?
Well, some of them are,
- For many data structures, strings serve as the foundation, like suffix trees, suffix arrays, and many more.
- Strings give us advantageous rules for tackling complicated issues.
- Strings are adaptable and may be utilized in various applications.
- Strings are human-readable.
What is a subsequence of a string?
Firstly let’s understand what a sequence is. A sequence signifies the number of characters the user derives after they delete 0 or more elements from the string. Note that the condition won’t alter the order of the remaining elements of the string.
Here’s an example:
Input: str= “XYZ”
Output:
X
XY
XYZ
XZ
Y
YZ
Z
Explanation:
As we know, once we delete certain characters, we will get a subsequence. And so, in this example, when 0 characters are deleted, we get XYZ.
Similarly, when we remove one character, we get the subsequences “xy” “xz” and “yz”. Then after deleting two characters, we get the subsequences “x”, “y” and “z”.
Finally we eradicate three characters, we get an empty subsequence of a string. Hence it is proven that we can maximum remove length-1 elements.
Approaches to find the subsequences of a string
To find a subsequence within a string, we can implement certain methods which consume less time and space.
Method 1: Pick and Don’t Pick
With this procedure, we first add the first element of the input string. The subsequence function is then used until the string becomes empty. The subsequence function is then used again, however, this time we do not attach the first item of the result string.
Steps:
- Initiate an input and output string, respectively.
- Delete the first character from the input string.
- Run the subsequence function by inserting the first character of input str.
- Print output strn and click return when input str turns empty.
- Likewise, execute the subsequence function without attaching the first element of input str to output str.
- Continue until the input str variable becomes null.
Time and Space:
With the recursive function included, the time consumed is O(2n), and as no extra space is wasted, the space complexity is O(1).
Method 2: Bit Manipulation Approach
In this technique, we will keep track of the integers and evaluate the set bits for each of them. We will then select the index elements for which we have a set bit.
This function returns a subsequence for each integer, followed by the string’s subsequence.
Steps:
- To begin, make an output list in which you’ll save all the substrings.
- Begin a loop for (1<<len).
- Begin a loop for the string length for each num value.
- Check to see if the ith bit is set.
- Add the character to the input string if the ith set bit is ready.
- If the length of the generated string after the inner loop procedure seems to be more than zero, add it to the output list.
Time and Space:
We will acquire a time complexity of O(2n) * O(n) due to the runtime of the nested loops (including outer and inner). Now that we’ve stored the substrings in a list, the space required is O(2n).
Extra Learning
Now that we have learned about string and how we can find the subsequences, here is another interesting concept to learn: the painter partition problem.
In this problem, we will be given the length of the boards and the painters available. We will be asked to find the amount of time each painter will take to paint on their respective boards.
To solve the painter partition problem, there are excellent methods which are:
- Brute Force: We will take note of all the possible contiguous partition sets, calculate the each case’s max sum partition and return the value
- Binary Search: We will categorize the maximum time to each painter and divide the sections that are remaining. This method also saves time and space.
Wrap-up
Strings are seen everywhere around us and in the computer world, it has a significant role. With the help of coding, we can easily derive many subsequences of a string, in one go.
We hope our guide gave you a great in-depth insight into the question. We wish you all the best in your interview preparation!
Also Read – Find A Pair With The Given Sum In An Array