HERE IS THE LINK TO MY CODE TEST CASE 1 IS SHOWING TIME ERROR ON RUNNING KINDLY TELL THE MISTAKE…
String sort run error
Hello @Madhavendra-Gupta-2240591552663161,
There are two issues with your stringsort() function:
-
Run-error
Reason:
There is no return statement for the case when l1>l2. -
Wrong Answer
Reason:
You are comparing only the first character and then comparing the lengths.
Example:
4
ab
apple
bat
batman
Your Output:
apple
ab
batman
bat
Expected Output:
ab
apple
batman
bat
Hope, this would help.
Give a like if you are satisfied.
edit my code please i am not able to understand how to do the needfull
Sure @Madhavendra-Gupta-2240591552663161,
I have modified the stringsort() function and it is passing all the test cases now:
Hope, this would help.
Give a like if you are satisfied.
not able to get anything u wrote in the code… can you please explain or write the easy code so that it is understandable…
Hey @Madhavendra-Gupta-2240591552663161,
Let’s understand that segment of code:
bool stringsort(string a,string b)
{
int i=0;
#To compare the corresponding elements of both the strings.
#This will overcome the error specified in point 2. in my previous reply.
while(a[i]==b[i]) i++;
#if all elements of string a are in string b, then a is a prefix of b
#return false i.e. a will come after b in the sorted sequence
if(a[i]==’\0’) return false;
#if all elements of string b are in string a, then b is a prefix of a
#return true i.e. a will come before b in the sorted sequence
else if(b[i]==’\0’) return true;
#else i.e. if neither a is prefix of b or b is prefix of a
#return true if a is lexicographicaly smaller than b else return false.
return a < b;
}
Hope, this would help.
Give a like if you are satisfied.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
Hello @Madhavendra-Gupta-2240591552663161,
As you have reopened the doubt,
please let me know if you still have any doubt left.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.