I see that this code is accepted, then I want to ask judge to increase the limit time for C++, because I have written almost the same idea, but I am struggling for hours with time limit, and cannot pass, or if you could see some differences, I appreciated if you could mention it. Actually I believe that my code should be faster than what he did as I return back the result the moment I find the result, however, he need to traverse the whole l/2 element in the for loop, but still it does not pass the time limit

class Solution {
public:
string shortestPalindrome(string s) {
int n=s.size(), mid=(n+1)/2;
string temp;
for(int i=mid; i>=1; --i){
if(expand(s, i-1, i, temp)){
return temp;
}
if(expand(s, i-1, i-1, temp)){
return temp;
}
}
}
private:
bool expand(string& s, int l, int r, string& temp){
int n=s.size();
if(n%2==1 && r==(n+1)/2) return false;
while(l>=0 && r<n && s[l]==s[r]){
l--;
r++;
}
l++; r--;
if(l!=0) return false;
string first_part=s.substr(r+1, n-(r+1));
reverse(first_part.begin(), first_part.end());
temp=first_part+s;
return true;
}
};