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...
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 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...
this is my code I want the opposite i want to convert a postfix expression to...
this is my code I want the opposite i want to convert a postfix expression to infix expression #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c; } char pop(Stack *st) {   char c = st->s[st->top];   st->top--;   //(A+B)*(C+D)   return c; } /* function to check whether a character is an operator or not. this function...
Please use c++ and follow the instruction I have written my own code but somehow I'm...
Please use c++ and follow the instruction I have written my own code but somehow I'm confused during the process I cannot figure out how to finish this lab, I need help. Write a program that allows user to input four floating-points values and then prints the largest. It must use functions max2 (given below) to determine the largest value. Feel free to use additional functions, but you are not required to do so. Hint: main will call max2 three...
Program Language C++ How do I take lines of string from an input file, and implement...
Program Language C++ How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file. Ex: Input.txt postfix: BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D / E+ Command.txt printList printListBackwards Output.txt List: postfix:BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D/E+ Reversed List: postfix:AB-CF*-D/E+ postfix:AB-C-D/ postfix:FE-C/B*A+ prefix:+A*B/C-EF postfix:BAC-*
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT