Question: Task 2 You are given a string S consisting of N lowercase English letters. In how many ways can we split Sinto two non-empty parts,

Task 2 You are given a string S consisting of N lowercase English letters. In how many ways can we split Sinto two non-empty parts, such that in at least one part the letter 'X' and the letter 'y' occur the same number of times? Write a function: int solution(string &s); that, given a string S of length N, returns the number of splits S satisfying the condition above. Examples: 1. Given S = "ayxbx", the function should return 3. There are four possible splits of S: "a/yxbx", "ay/xbx", "ayx/bx" and "ayxb/x". Only "ay/xbx" does not fulfill the condition, so the answer is 3. Note that in "a/yxbx" the left part has occurrences of 'X' and 'y', so it counts as correct split. 2. Given S = "xzzzy", the function should return 0. 3. Given S = "toyxmy", the function should return 5. 4. Given S="apple", the function should return 4. Write an efficient algorithm for the following assumptions: Any problems with the editor
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
