[problem:A] If the strings are the same, Any subsequence of a is indeed a subsequence of b so the answer is "-1", otherwise the longer string can't be a subsequence of the other (if they are equal in length and aren't the same, No one can be a subsequence of the other) so the answer is maximum of their sizes.
Time complexity : O(|a| + |b|).
Problem author : me.
Solution author : me.
Testers : me and mahmoudbadawy.
[problem:B]
First solution:-
Let x, y and z be the lengths of 3 line segments such that x ≤ y ≤ z, If they can't form a non=degenerate triangle, Line segments of lengths x - 1, y and z or x, y and z + 1 can't form a triangle, So we don't need to try all the combinations, If we try y as the middle one, We need to try the maximum x that is less than or equal to y and the minimum z that is greater than y, The easiest way to do so is to sort the line segments and try every consecutive 3.
Time complexity : O(nlog(n)).
Second solution:-
Depending on the note from the first solution, If we try to generate a sequence such that after sorting every consecutive 3 line segments will form a degenerate triangle, It will be 1 1 2 3 5 8 13 ... which is Fibonacci sequence, Fibonacci is a fast growing sequence, fib(45) = 1134903170, Notice that Fibonacci makes maximum n with "NO" as the answer, That means the answer is indeed "YES" for n ≥ 45, For n < 45, You can do the naive O(n3) solution or the first solution.
let x be the number that satisfies these inequalities:-
fib(x) ≤ maxAi.
fib(x + 1) > maxAi.
Time complexity : O(x3) or O(xlog(x)).
Problem author : me.
Solutions author : me.
Tester : me and mahmoudbadawy.
[problem:C]
Let dp[i] be the number of ways to split the prefix of s ending at index i into substrings that fulfills the conditions. Let it be 0-indexed. Our base case is dp[0] = 1. Our answer is dp[n - 1]. Now let's calculate it for every i. Let l be the minimum possible index such that the substring from l to i satisfies the condition, Let x be a moving pointer, At the beginning x = i - 1, every time we move x we calculate new l depending on the current character like that l = max(l, i - as[x]). While x is greater than or equal to l we add dp[x] to dp[i], To find the longest substring find maximum i - l, To find the minimum number of substrings, there is an easy greedy solution, Find the longest valid prefix and delete it and do the same again until the string is empty, The number of times this operation is repeated is our answer.
Time complexity : O(n2).
Try to find O(n) solution(I'll post a hard version of some problems on this blog soon).
Problem authors : me and mahmoudbadawy.
Solution author : me and mahmoudbadawy.
Testers : me and mahmoudbadawy.