Question

In: Computer Science

Write a C++ program that uses a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates...

Write a C++ program that uses a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates in a C++ string. Consecutive duplicates are a pair of duplicate English alphabets sitting next to each other in the input string. Example: "AA", "KK", etc., are all consecutive duplicates. This function will internally run as many iterations as needed to remove all consecutive duplicates until there is either no consecutive duplicates left, or the string becomes empty (in which the function returns "Empty" back to the user):

string deduplicate(string input)

Your main() may look like the following, or anything you prefer to put in it as it is not graded:

int main() {
  cout << deduplicate("AABB"); // should output "Empty"
  cout << deduplicate("A"); // "A"
  cout << deduplicate("ABBA"); // should output "Empty"
  cout << deduplicate("AAA"); // "A"
  cout << deduplicate("AKA"); // "AKA" because there is no consecutive pair.
  return 0;
}

-----------------------------------------------------------

Examples:

  • "KCCK" -> first iteration results in "KK" by removing "CC" as consecutive duplicates -> second iteration starts with "KK" as consecutive duplicates hence removed. Function returns "Empty".
  • "ZZZ" -> first iteration results in "Z" by removing the first "ZZ". There is nothing to be removed after that. Function returns "Z".
  • "KKCCD" -> first iteration removes "KK" resulting in "CCD" -> second iteration removes "CC" resulting in "D". There is nothing left to be removed. Function return "D".

-----------------------------------------------------------

Constraints/Assumptions:

  • Input string consists of only uppercase English alphabets, "A" - "Z".
  • Input string consists of at least 1 English alphabet.
  • Input string consists of at most 100 alphabets.
  • Function returns the non-reducible string.
  • If the reduced string is empty, function returns the string "Empty".

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

using namespace std;

//required method

string deduplicate(string input){

                //a flag denoting if duplicate is found, setting to true initially

                bool duplicates=true;

                //looping as long as duplicates is true

                while(duplicates){

                                //at the start of each loop, initially setting duplicates to false

                                duplicates=false;

                                //a string to store the updated text

                                string updated="";

                                //looping through each index in input string

                                for(int i=0;i<input.length();i++){

                                               //checking if next index is valid, and characters at indices i and i+1 are same

                                               if((i+1)<input.length() && input[i]==input[i+1]){

                                                               //setting duplicates to true

                                                               duplicates=true;

                                                               //advancing i, skipping next character

                                                               i++;                                       

                                               }else{

                                                               //otherwise simply appending current character to updated

                                                               updated+=input[i];

                                               }

                                }

                                //at the end of each iteration of outer loop, setting updated as input (for next loop)

                                input=updated;

                }

                //at the very end, if input is empty, returning "Empty", else returning input

                if(input==""){

                                return "Empty";

                }else{

                                return input;

                }

}

int main(){

                //testing

                cout << deduplicate("AABB")<<endl; // should output "Empty"

                cout << deduplicate("A")<<endl;// "A"

                cout << deduplicate("ABBA")<<endl; // should output "Empty"

                cout << deduplicate("AAA")<<endl; // "A"

                cout << deduplicate("AKA")<<endl; // "AKA" because there is no consecutive pair.

                cout << deduplicate("ZZZ")<<endl;// "Z"

                cout << deduplicate("KKCCD")<<endl;// "D"

                return 0;

}

/*OUTPUT*/

Empty

A

Empty

A

AKA

Z

D


Related Solutions

in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
P4.7 Following Section 4.9, develop a program that reads a string and removes all duplicates. For...
P4.7 Following Section 4.9, develop a program that reads a string and removes all duplicates. For example, if the input is Mississippi, print Misp. Start small and just print the first letter. Then print the first letter and true if the letter is not duplicated elsewhere, false otherwise. (Look for it in the remaining string, by using the substring and indexOf method). Next, do the same for the first two letters, and print out for each letter whether or not...
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a...
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a specific character from a string b. Write the pseudocode for the program.
Create a program in C++ implementing the LinkedStack 7. Write a client that removes all negative...
Create a program in C++ implementing the LinkedStack 7. Write a client that removes all negative numbers from a stack of int objects. If the original stack contained the integers 30, –15, 20, –25 (top of stack), the new stack should contain the integers 30, 20.
Write a C++ program that uses all the relational operators.
Write a C++ program that uses all the relational operators.
write a c++ member function that removes the first instance of a specific element in a...
write a c++ member function that removes the first instance of a specific element in a linked list and then return the size of the list after the removal whether it was successful or not.
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a...
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a linked list. After attemtped removal return the SIZE of the linked lost whether or not the removal was successful.
C++ Class involving duplication The goal is to make add() not allow duplicates. Bag<int> bag1; bag1.add(1);...
C++ Class involving duplication The goal is to make add() not allow duplicates. Bag<int> bag1; bag1.add(1); //ok bag1.add(2); //ok bag1.add(1); //ignore but do not break or terminate. The submission will go through but will not get added to array #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag { public: Bag(); int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); //Modify this bool remove(const T& an_entry); /** @post item_count_ == 0 **/ void clear(); /**...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT