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?
The following code must be written in Matlab I want to print the following in Matlab...
The following code must be written in Matlab I want to print the following in Matlab (x1,x2, x3) = (0.33333, 0.33333, 0.33333)  . The whole thing should be on the same line. I need to use fprintf and write out the coordinates with 5 decimal places of variable x = (0.33333, 0.33333, 0.33333) Thanks!
In python I have my code written and I want just 4 functions to be fix...
In python I have my code written and I want just 4 functions to be fix in my code according to rule. My code is running but it has some problem regarding to rules. (I have did all the other things so you do not have to worry about other functions) 1) all the players has to play until he/she reaches to at least 500 points in first round. When user reach 500 points for the first time, user may...
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...
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 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.
I need convert this java code to C language. There is no string can be used...
I need convert this java code to C language. There is no string can be used in C. Thank you! import java.util.Scanner; public class Nthword { public static void main( String args[] ) { String line; int word; Scanner stdin = new Scanner(System.in); while ( stdin.hasNextLine() ) { line = stdin.nextLine(); word = stdin.nextInt(); stdin.nextLine(); // get rid of the newline after the int System.out.println( "Read line: \"" + line + "\", extracting word [" + word + "]" );...
I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS ....
I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS . Add the following functions to the class arrayListType: Then, update the main function to test these new functions. removeAll - which removes ALL of the instances of a value in the list min - which returns the minimum value in the list max - which returns the maximum value in the list arrayListType.h : #ifndef H_arrayListType #define H_arrayListType class arrayListType { public: bool isEmpty()...
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...
I WANT THIS CODE TO BE SIMPLE BECAUSE I AM NEW TO CODING AND I WANT...
I WANT THIS CODE TO BE SIMPLE BECAUSE I AM NEW TO CODING AND I WANT TO KNOW THE DETAILS! straight C Program. write a quiz game where a number of questions are asked and the user will win each time he answers right. and he loses each time he answers wrong. the type of questions asked can be about sports or anything else. You can put 5 quiz questions. It should only include the following: #include <stdio.h> and #include...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT