i am getting run error in some of the cases.
ide: https://ide.codingblocks.com/s/191882
Are the same - divide and conquer
Hey @Vishal123
There’s a run error because of the following statements.
string a1 , b1 , a2 , b2;
and
for(int i=1 ; i<=mid ; i++){
a1[i] = s1[i];
b1[i] = s2[i];
}
for(int i=mid+1 ; i<=s1.length() ; i++){
a2[i] = s1[i];
b2[i] = s2[i];
}
You can’t assign values to indexes of empty strings. You’ve to first initialize the strings, and then add elements to them.
You should do this like:
string a1="" , b1="" , a2="" , b2="";
and
for(int i=0 ; i<=mid ; i++){
a1+= s1[i];
b1+= s2[i];
}
for(int i=mid+1 ; i<=s1.length() ; i++){
a2+= s1[i];
b2+= s2[i];
}
Also you started first for loop from index 1, start it from 0.
@Vishal123
You’re writing:
for(int i=0 ; i<=mid ; i++){
a1[i] += s1[i];
b1[i] += s2[i];
}
for(int i=mid+1 ; i<=s1.length() ; i++){
a2[i] += s1[i];
b2[i] += s2[i];
}
You’ve to write:
for(int i=0 ; i<=mid ; i++){
a1+= s1[i];
b1+= s2[i];
}
for(int i=mid+1 ; i<=s1.length() ; i++){
a2+= s1[i];
b2+= s2[i];
}
output is wrong in last test case otherwise all cases are passed . ide: https://ide.codingblocks.com/s/193469
Hey @Vishal123
the question says that:
They are equal. 2 .If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2, then one of the following is correct: 1 . a1 is equivalent to b1, and a2 is equivalent to b2 2 . a1 is equivalent to b2, and a2 is equivalent to b1
You not only need to compare a1, b2 and a2, b1, the strings are also equivalent if a1 is equivalent to b2 or a2 is equivalent to b1 acc to the ques, so you need to put a recursive call after your both if statements:
if(are_they_same(a1, b1)||are_they_same(a2, b1)) return true;