Problem Statement: Shashank likes strings in which consecutive characters are different. For example, he likes ABABA, while he dosen't like ABAA. Given a string congaing characters A and B only, he wants to change it to a string he likes. To do so he is allowed to delete the characters in the string. The problem is to fin d the minimum number of deletion required.
For example:
ABBAB -> ABAB (Requires 1 deletion)
AAAAAA -> NULL (Requires 6 deletion)
ABABA -> ABABA (Requires 0 deletion)
C++ Code:
ABBAB -> ABAB (Requires 1 deletion)
AAAAAA -> NULL (Requires 6 deletion)
ABABA -> ABABA (Requires 0 deletion)
- #include <iostream>
- #include <string>
- #include <cstring>
- using namespace std;
- int delRequired(string testString)
- {
- size_t size = testString.length();
- char arr[size+1];
- strcpy(arr,testString.c_str());
- int i, noOfDel = 0;
- for(i = 0; i < size; ++i) {
- if(arr[i] == arr[i+1]) {
- noOfDel += 1;
- }
- }
- return noOfDel;
- }
- int main()
- {
- int testCase, i = 0;
- cin >> testCase;
- int delArr[testCase];
- while(testCase--) {
- string testString;
- cin >> testString;
- delArr[i++] = delRequired(testString);
- }
- delArr[i] = -1;
- i = 0;
- while(delArr[i] != -1) {
- cout << delArr[i] << endl;
- i += 1;
- }
- return 0;
- }