Knowee
Questions
Features
Study Tools

Given a pair of strings of equal lengths, Geek wants to find the better string. The better string is the string having more number of distinct subsequences. If both the strings have equal count of distinct subsequence then return str1.Example 1:Input:str1 = "gfg", str2 = "ggg"Output: "gfg"Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences. Example 2:Input: str1 = "a", str2 = "b"Output: "a"Explanation: Both the strings have only 1 distinct subsequence. Your Task:You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.Expected Time Complexity: O( max( str1.length, str2.length ) )Expected Auxiliary Space: O( max( str1.length, str2.length ) )Constraints:1 <= str1.lenght , str2.length <= 30

Question

Given a pair of strings of equal lengths, Geek wants to find the better string. The better string is the string having more number of distinct subsequences. If both the strings have equal count of distinct subsequence then return str1.Example 1:Input:str1 = "gfg", str2 = "ggg"Output: "gfg"Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences. Example 2:Input: str1 = "a", str2 = "b"Output: "a"Explanation: Both the strings have only 1 distinct subsequence. Your Task:You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.Expected Time Complexity: O( max( str1.length, str2.length ) )Expected Auxiliary Space: O( max( str1.length, str2.length ) )Constraints:1 <= str1.lenght , str2.length <= 30

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, we need to create a function that calculates the number of distinct subsequences for each string and then compares them. Here is a Python solution:

def distinct_subsequences(s):
    n = len(s)
    dp = [0 for _ in range(n+1)]
    last = [-1 for _ in range(256)]
    dp[0] = 1
    for i in range(1, n+1):
        dp[i] = 2*dp[i-1]
        if last[ord(s[i-1])] != -1:
            dp[i] = dp[i] - dp[last[ord(s[i-1])]]
        last[ord(s[i-1])] = i-1
    return dp[n]

def betterString(str1, str2):
    if distinct_subsequences(str1) >= distinct_subsequences(str2):
        return str1
    else:
        return str2

The distinct_subsequences function calculates the number of distinct subsequences of a string using dynamic programming. The betterString function uses this function to compare the number of distinct subsequences of str1 and str2 and returns the better string. The time complexity is O(max(str1.length, str2.length)) and the auxiliary space complexity is also O(max(str1.length, str2.length)).

This problem has been solved

Similar Questions

Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.For example, "ace" is a subsequence of "abcde".A common subsequence of two strings is a subsequence that is common to both strings. Example 1:Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3.

Given 2 strings A and B, check if A is present as a subsequence in B.

You are given an array of strings arr. A string s is formed by the concatenation of a subsequence of arr that has unique characters.Return the maximum possible length of s.A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. Example 1:Input: arr = ["un","iq","ue"]Output: 4Explanation: All the valid concatenations are:- ""- "un"- "iq"- "ue"- "uniq" ("un" + "iq")- "ique" ("iq" + "ue")Maximum length is 4.

Given a string s of length n, find all the possible subsequences of the string s in lexicographically-sorted order.

Raju is preparing questions for the internal test. While creating questions, he gave one such question to his son Arun to solve. Raju has given the string S to his son Arun. Arun's task is to print the size of the longest possible substring that has exactly K unique characters.ExampleInput:String = aabacbebebe; K = 3Output:The substring will be cbebebe with length 7. Explanation:The unique characters in the string are: 'a', 'b', 'c', and 'e'. The longest subsequence with at most 3 distinct characters is "aabacbe". It contains the characters 'a', 'b', and 'c', which is the maximum number of distinct characters allowed (k = 3). Therefore, the length of the longest subsequence is 7.Input format :The first line of input consists of the string S.The second line of input consists of the integer K.Output format :The output prints the size of the longest possible substring that has exactly K unique characters.Refer to the sample output for the formatting specifications.Code constraints :1≤ string length ≤ 501≤ K ≤10Sample test cases :Input 1 :aabacbebebe3Output 1 :7Input 2 :abbc1Output 2 :2

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.