Help with Lapindromes Code:-Lapin

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 :grin: :v:

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');

// your code goes here
// 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";
}

Please help me!!

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 :man_shrugging:

Help

1
bbdcbdb

That’s not how you format your code (why do I keep seeing people do this??) - this is :slight_smile:

sorry for that but can you help me out

Did you try that testcase I gave you?

it ouputs NO

Thanks

1 Like