# Help with Lapindromes Code:-Lapin

here is my code. I am not able to figure out why is my code getting a wrong answer?

Consider the test input:

``````1
ddbebggde
``````

it should return NO, right?
My Code is returning that but not able to find what is wrong!
Approach is to find if the string is odd length, then remove the mid element. If not odd proceed.
Then divide the string in two halves and create hash map of each character on both sides.
Then pick left or right hash and compare the existence and frequency in other hash.

For roort this testcase your code is printing both YES and NO

Instead, why canâ€™t you insert half of the characters into the map and check the other half characters are present in the map or not.

thank you so much for this test case!!

I am still wondering that what was your thought process for this test case! Any suggestions would be awesome and helpful

My pleasure

sorry for thatâ€¦ i donâ€™t know why it is showing that

Iâ€™ve gone through your code, after looking at the else part I was sure that, for test cases such as rotor, your approach might fail because if length is odd you are starting the comparison from the middle element itself, so Iâ€™ve jumbled the word rotor to roort and as Iâ€™ve expected it popped a wrong answer.

1 Like
C Code
``````void solve() {
// Solve test cases here
//
char s[size];
scanf("%s", s);
int freq1[26];
int freq2[26];
for(int i = 0; i < 26; i++)
freq1[i] = freq2[i] = 0;
int n = strlen(s);
for(int i = 0; i < n/2; i++) {
freq1[s[i] - 'a'] ++;
freq2[s[n - i - 1] - 'a'] ++;
}
bool flag = true;
for(int i = 0; i < 26; i++) {
if(freq1[i] != freq2[i]) {
flag = false;
break;
}
}
printf(flag?"YES\n":"NO\n");
}
``````

taken from

Hi,
Taking the test case I ran with the code. And I have also done the test case check with mulitple ways and I am getting expected output. For roort as well I am getting a â€śNOâ€ť one time. I did not do any code change.
I am stuck with this question.
My code(Node JS):

``````process.stdin.resume();
process.stdin.setEncoding('utf8');

// input construct starts below
let input = "";
process.stdin.on("data", (res) => input = res);
process.stdin.on("end", call => cleanAndSend());

function cleanAndSend() {
let splitted = input.split("\n");
logicFunction(splitted);
}

function logicFunction(data) {
let [T, ...values] = data;
T = parseInt(T);
for(let i=0; i<T; i++) {
let string = values[i];
console.log(lapString(string, string.length));
}
}

// input construct ends above
function midIgnoredString(string) {
let len = string.length;
let mid = Math.floor((0 + (len - 1)) / 2);
let splitted = string.split("");
splitted[mid] = '';
return splitted.join("");
}
function getHash(string, mid) {
let leftHash = {};
let rightHash = {};
for (let i = 0; i <= mid; i++) {
if (leftHash[string[i]]) {
leftHash[string[i]] = leftHash[string[i]] + 1;
} else {
leftHash[string[i]] = 1;
}
}
for (let i = mid + 1; i <= string.length - 1; i++) {
if (rightHash[string[i]]) {
rightHash[string[i]] = rightHash[string[i]] + 1;
} else {
rightHash[string[i]] = 1;
}
}

return [leftHash, rightHash];

}

function lapString(string, len) {
if(len === 1) return "NO";
if((string.length) % 2 !== 0) {
string = midIgnoredString(string)
len = string.length;
}
let mid = Math.floor((0 + (len - 1)) / 2);
let [leftHash, rightHash] = getHash(string, mid);
// console.log(leftHash, rightHash);
for (let key in leftHash) {
if(rightHash[key] === leftHash[key]) continue;
return "NO";
}
return "YES";
}
``````

Hi,
Thank you for the solution. I would do this way, However I have followed the hash approach and mentioned my code in comment before this one. Pl help me out if you can(Nodejs). I just want to know what I am doing wrong in my approach if i am getting an the expected output in every test case that I can think of.

I canâ€™t see any submissions from you here:

Where are you submitting to, exactly?

1 Like

seriously!! I have also seen that there are no submissions on the link you provided.
PFB screen shot where it shows my submissions

I donâ€™t why it is not showing
I am doing this via DSA learning series page though

may be this should open(my username: anshup7)
All submissions

Thanks - the main reason I asked is that I canâ€™t find anything wrong with your code except that it doesnâ€™t handle the case where an input string has leading or trailing spaces, so Iâ€™m confused as to why itâ€™s failing.

Maybe you could try trimming leading/ trailing spaces from a string when you process it?

ok cool will check that, thank you
one more thing, do you have any idea why i canâ€™t see other people solution in my choice of programming language?
Is that a rule of codechef?

The DSA Learning Series usually let you see other peopleâ€™s solutions, but that doesnâ€™t seem to be the case at the moment. No idea why

Help