Simple GUI Notepad Using Ruby

GUI Notepad Using Ruby Code require 'tk' class Notepad def saveFile file = File.open("note", "w") ...

Thursday, January 29, 2015

Programming Problem Two

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:

    1. #include <iostream>
    2. #include <string>
    3. #include <cstring>
    4. using namespace std;

    5. int delRequired(string testString)
    6. {
    7. size_t size = testString.length();
    8. char arr[size+1];
    9. strcpy(arr,testString.c_str());
    10. int i, noOfDel = 0;
    11. for(i = 0; i < size; ++i) {
    12. if(arr[i] == arr[i+1]) {
    13. noOfDel += 1;
    14. }
    15. }
    16. return noOfDel;
    17. }

    18. int main()
    19. {
    20. int testCase, i = 0;
    21. cin >> testCase;
    22. int delArr[testCase];
    23.  
    24. while(testCase--) {
    25. string testString;
    26. cin >> testString;
    27. delArr[i++] = delRequired(testString);
    28. }
    29. delArr[i] = -1;
    30. i = 0;
    31. while(delArr[i] != -1) {
    32. cout << delArr[i] << endl;
    33. i += 1;
    34. }
    35. return 0;
    36. }