Fails a test case on submission, but runs perfectly fine on machine!


  • 1
    C

    Hi,

    Here's my solution for the problem.

    char* convert(char* s, int numRows){
    if(numRows == 1)
        return s;
    int length_s = 0, diff = 0, index = 0, i, j=0;
    int toggle = 0;
    while(*(s + length_s) != '\0')
        length_s ++;
    char* converted_string = (char*)malloc(length_s);
    diff = (numRows * 2) - 2;
    for(i = 1; i <= numRows; i++){
    	toggle = 0;
    	index = i;
    	while(index <= length_s){
    		if(i == 1 || i == numRows){
    			converted_string [j] = s[index - 1];
    			index = index + diff;
    		} else{
    			converted_string[j] = s[index - 1];
    			if(toggle == 0)
    				index = index + ((numRows - i)*2);
    			else
    				index = index + (diff - ((numRows - i)*2));
    			if(toggle == 0)
    			    toggle = 1;
    			else 
    			    toggle = 0;
    		}
    		j++;
    	}
    }
    	return converted_string;
    }
    

    and the ideone link for the same, http://ideone.com/kd4SXT

    The above solution cleared 594 test cases out of 1158.

    However, it fails over a test case.

    input: "Apalindromeisaword,phrase,number,orothersequenceofunitsthatcanbereadthesamewayineitherdirection,withgeneralallowancesforadjustmentstopunctuationandworddividers.", 2

    Output:
    "Aaidoeswr,haenme,rtesqecouishtabrateaeaietedrcinwtgnrlloacsoajsmnsoucutoadodiiesplnrmiaodprs,ubroohreunefnttacneedhsmwynihrieto,iheeaalwnefrdutettpntainnwrdvdr.,withgen�

    Excepted output:
    "Aaidoeswr,haenme,rtesqecouishtabrateaeaietedrcinwtgnrlloacsoajsmnsoucutoadodiiesplnrmiaodprs,ubroohreunefnttacneedhsmwynihrieto,iheeaalwnefrdutettpntainnwrdvdr."

    For the same input, it runs perfectly well on ideone giving the expected output.

    Can someone please help with this?

    Thanks in advance. :)


  • 0
    S

    I got the same issue. I am also using C. Not sure why.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.