Question

In: Computer Science

I want this code to be written in c++. Take a string of length n as...

I want this code to be written in c++.

Take a string of length n as an input from the user such that n>=8 and only lower-case letters (a-z) are allowed as valid string characters.

Deleting a letter from the string removes all the occurrences of that letter.

The objective is to find the longest possible string such that it is left with only two unique letters and no two consecutive characters
in a string are the same.

If there exist more than one valid longest strings, you need to print any one of those.

The solution should be implemented using pointers

Examples:

Input string: abbdacbdac - > Output string: adada

Input string: cxtdacxdacd - > Output string: cdcdcd

Solutions

Expert Solution

SOLUTION -

#include<bits/stdc++.h>
using namespace std;
int main() {

string s;
int N; //length of string
cin >> N >> s;
vector<char> m;
for(int i = 0; i < N; ++i)
m.push_back(s[i]);
sort(m.begin(), m.end());
m.erase(unique(m.begin(), m.end()), m.end());
int X = m.size();
bool poss = false;
string ans_string;
for(int i = 0; i < X; ++i) {
for(int j = 0; j < X; ++j) {
if(i == j) continue;
char st = m[i], ot = m[j];
int k = 0;
bool curr = true, start = true;
string curr_string;
while(k < N) {
if(s[k] != st && s[k] != ot){
++ k;
continue;
}
if(start) {
if(s[k] != st) {
curr = false;
break;
}
curr_string.push_back(st);
} else {
if(s[k] != ot) {
curr = false;
break;
}
curr_string.push_back(ot);
}
start = !start;
++ k;
}
if(curr && ans_string.size() < curr_string.size()) {
poss = true;
ans_string = curr_string;
}
}
}
if(!poss) cout << "String Not Found";
else
cout << ans_string;
return 0;
}

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

c++ I want to flip the string when it's string type. For example, if it is...
c++ I want to flip the string when it's string type. For example, if it is 'apple', I would like to print 'elppa'. how can i do?
I want the code below to be edited: Rather than giving the input string inside the...
I want the code below to be edited: Rather than giving the input string inside the code, I want the program to ask the user for an input and calculate and complete this code. I have pasted the actual code below, Please edit the input section only so that I can input any string or any sentence as I like. The program must ask the user that "Enter a string/sentence" and take the data to calculate the Huffman code. #include...
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
C++ Code! This code was written/implemented using the "class format." How would I go about in...
C++ Code! This code was written/implemented using the "class format." How would I go about in converting it to the "struct format?" #include <iostream> #include <iomanip> using namespace std; class ListNode{ public: string course_name; string course_number; string course_room; ListNode* next; ListNode(){ this->next = NULL; } ListNode(string name, string number, string room){ this->course_name = name; this->course_number = number; this->course_room = room; this->next = NULL; } }; class List{ public: ListNode* head; List(){ this->head = NULL; } void insert(ListNode* Node){ if(head==NULL){ head...
I have written code in C programming that checks where the command line arguments are floats...
I have written code in C programming that checks where the command line arguments are floats or not. For example, if I type "./math 1 1 0 0 2.5 3" in the terminal, my program realizes they are all floats but if I type "./math 1 1 0 0 2.5 g", it recognizes that not all arguments are floats and gives an error message. I want to take my code further such that after typing in "./math 1 1 0...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
So I have written a code for it but i just have a problem with the...
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
C++, Write a function, noPunct, that would take a string, go through the string character by...
C++, Write a function, noPunct, that would take a string, go through the string character by character, and push any character that is NOT a punctuation mark onto a stack but count the punctuation dropped. After going through the entire string, print the contents of the stack. Then print the number of punctuation dropped. Hint: use ispunct() library function (returns true if character is punctuation)
Compression of a bit string x of length n involves creating a program shorter than n...
Compression of a bit string x of length n involves creating a program shorter than n bits that returns x. The Kolmogorov complexity of a string K(x) is the length of shortest program that returns x (i.e. the length of a maximally compressed version of x). (a) Explain why "the smallest positive integer not definable in under 100 characters" is paradoxical. (b) Prove that for any length n, there must be at least one bit string that cannot be compressed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT