Problem StatementYou are given a string s of length n. You want to find all the distinct non-empty strings that can be formed from s such that:No two adjacent characters from the string s are chosen in the string that we make from s.Since the answer can be large print it modulo 109+7.Input FormatThe only line of input contains a string s.Constraints1 <= n <= 105Output FormatReturn an integer as asked.
Question
Problem StatementYou are given a string s of length n. You want to find all the distinct non-empty strings that can be formed from s such that:No two adjacent characters from the string s are chosen in the string that we make from s.Since the answer can be large print it modulo 109+7.Input FormatThe only line of input contains a string s.Constraints1 <= n <= 105Output FormatReturn an integer as asked.
Solution
The problem statement is asking to find all the distinct non-empty strings that can be formed from a given string such that no two adjacent characters from the original string are chosen in the new string. The answer can be large so it should be printed modulo 109+7.
Here is a step-by-step solution:
-
Initialize two variables,
prevandcurr, to keep track of the number of distinct strings that can be formed using the characters up to the current position. Setprevto 1 andcurrto 2 (since a string of length 1 can form 2 distinct strings: the string itself and an empty string). -
Iterate over the characters in the string from the second character to the end. For each character, calculate the new
curras the sum of the currentcurrandprev, modulo 109+7. This is because the number of distinct strings that can be formed using the characters up to the current position is the sum of the number of distinct strings that can be formed using the characters up to the previous position (since we can choose not to use the current character) and the number of distinct strings that can be formed using the characters up to the position before the previous position (since we can choose to use the current character, but then we cannot use the previous character). Then, updateprevto the oldcurr. -
After the loop,
curris the total number of distinct strings that can be formed using the characters in the string. However, this includes the empty string, which is not considered a valid string according to the problem statement. Therefore, subtract 1 fromcurrto exclude the empty string. -
Finally, return
currmodulo 109+7 to ensure that the result fits within the required range.
Here is a Python solution based on the above steps:
def count_strings(s):
MOD = 10**9 + 7
prev, curr = 1, 2
for i in range(1, len(s)):
prev, curr = curr, (prev + curr) % MOD
return (curr -
Similar Questions
Problem StatementYou wish to help Ashish, who possesses a collection of N strings, some of which may be duplicated, and has been assigned the task of finding the kth unique string.If the number of unique strings is less than k, he needs to display an empty string. Considering you are Ashish's best friend can you assist him with this challenge?Input FormatThe first line contains an integer N denoting the number of strings.The next N lines contain strings.The next line contains an integer k.Output FormatThe output contains the kth distinct string. If there are less than k unique string display an empty string.Constraints1<=N<=105-10^8<=arr[i].length()<=10^8Sample Testcase 0Testcase Input6dbcbca2Testcase OutputaExplanationThe only strings in arr that are distinct are "d" and "a." The letter "d" comes first, making it the first separate string.Because "a" appears second, it is the second distinct string. "a" is returned since k == 2.
Given two integers N and K, the task is to find the string S of minimum length such that it contains all possible strings of size N as a substring. The characters of the string should be from integers ranging from 0 to K-1. Example 1:Input:N = 2, K = 2Output: 00110Explanation: Allowed characters are from 0 to k-1 (i.e., 0 and 1).There are 4 string possible of size N=2 (i.e "00", "01","10","11")"00110" contains all possible string as a substring. It also has the minimum length.Example 2:Input:N = 2, K = 3Output: 0010211220Explanation: Allowed characters are from 0 to k-1 (i.e., 0, 1 and 2).There are total 9 strings possibleof size N, given output string has the minimumlength that contains all those strings as substring.Your Task: You don't need to read input or print anything. Complete the function findString( ) which takes the integer N and the integer K as input parameters and returns the string.Note: In case of multiple answers, return any string of minimum length which satisfies above condition. The driver will print the length of the string. In case of wrong answer it will print -1.Expected Time Complexity: O(KNlogK)Expected Space Complexity: O(KNN)Constraints:1 ≤ N ≤ 41 < K < 101 < KNN < 106
Max Score: 20Given a string S and a character C, check whether the character C is occurring together in the string S.Input FormatThe first and only line of input contains a string S and character, C separated by space.Output FormatPrint "Yes" if the character C is occurring together, otherwise print "No".Constraints1 <= |S| <= 103ExampleInputsnooze oOutputYes
Given 2 strings A and B, find the smallest substring of B having all the characters of A, in any order.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line containing 2 space-separated strings - A and B.Output FormatFor each test case, print the length of the smallest substring of B having all the characters of A, separated by newline. If no such substring is found, print -1.Constraints20 points1 <= T <= 1001 <= size(A), size(B) <= 10060 points1 <= T <= 1001 <= size(A), size(B) <= 1000120 points1 <= T <= 1001 <= size(A), size(B) <= 10000General Constraints'a' <= A[i], B[i] <= 'z'ExampleInput4fkqyu frqkzkruqmfqyuzlkygonmwvytbytn uqhmfjaqtgngcwkuzyamnerphfmwbloets lwbcrsfothplxseplrtbshbtstjloxsfdzpd dclzztpjldkndgbdqqzmbpOutput7-1139ExplanationTest Case 1:The smallest substring containing all characters of A is "fqyuzlk", which has a length of 7.Test Case 2:Despite considering all possible substrings of B, we cannot find any substring containing all characters of A.Test Case 3:The smallest substring containing all characters of A is "bcrsfothplxse", which has a length of 13.Test Case 4:The smallest substring containing all characters of A is "ztpjldknd", which has a length of 9.
Given a string of characters, find the first repeating character.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line contains a single string of characters.Output FormatFor each test case, print the first repeating character, separated by a new line. If there are none, print '.'.Constraints1 <= T <= 1000'a' <= str[i] <= 'z'1 <= len(str) <= 104ExampleInput4datastructuresalgorithmssmartinterviewshackerrankOutputa.sa
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.