In: Computer Science
c++
do add comments for better understanding
Lapindrome is defined as a string which when split in the
middle, gives two halves having the same characters and same
frequency of each character. If there are odd number of characters
in the string, we ignore the middle character and check for
lapindrome. For example gaga is a lapindrome, since the two halves
ga and ga have the same characters with same frequency. Also,
abccab, rotor and xyzxy are a few examples of lapindromes. Note
that abbaab is NOT a lapindrome. The two halves contain the same
characters but their frequencies do not match.
Your task is simple, declare a C++ Class Lapindrome that has a
function checkLapindrome which display Yes if the string is
Lapindrome otherwise, it should display No. The structure of the
Class is given below. You are required to implement every function
given in the class Lapindrome.
class Lapindrome
{
private:
int size; char* cstr;
public:
Lapindrome (int size=10); //initialize size and cstr
void setStr(char* cstr); //Assign the value of cstr to member
variable cstr
char* getStr();
//returns the string cstr. void checkLapindrome (); //Display Yes
if the cstr is Lapindrome otherwise No ~Lapindrome();
//Destructor
};
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
class Lapindrome
{
private:
int size; char* cstr;
public:
Lapindrome (int size,char*cstr){
this->size= size;
this->cstr = cstr;
}
void setStr(char* cstr){
this->cstr = cstr;
}
char* getStr(){
return this->cstr;
}
bool checkCorrectOrNot(char* s)
{
int count1[MAX_CHAR] = {0};
int count2[MAX_CHAR] = {0};
int n = strlen(s);
if (n == 1)
return true;
for (int i=0,j=n-1; i<j; i++,j--)
{
count1[s[i]-'a']++;
count2[s[j]-'a']++;
}
for (int i = 0; i<MAX_CHAR; i++)
{
if (count1[i] != count2[i])
{ return false; }
}
return true;
}
};
int main()
{
char array[] = "ababababaa";
char *pointer2 = array;
Lapindrome l1(15,pointer2);
if(l1.checkCorrectOrNot(pointer2)){
printf("true");
}
else{
printf("false");
}
return 0;
}