void solve(){
int n; cin>>n;
vi v(n); rep(i,0,n) cin>>v[i];
int flag=1;
rep(i,0,n/2){
if(v[i]!=v[n-1-i]) {
flag=0; break;
}
}
if(flag){
yes
return;
}
string s="";
for(int i=0;i<n;i++)
if(v[i]!=v[0]) s=s+to_string(v[i]);
string s2=s;
reverse(s2.begin(),s2.end());
if(s==s2)
{
yes return;
}
s="";
for(int i=0;i<n;i++)
if(v[i]!=v[n-1]) s=s+to_string(v[i]);
s2=s;
reverse(s2.begin(),s2.end());
if(s==s2)
{
yes return;
}
no
}
because of s=s+to_string(), instread of this , you can use s+=to_string().
generally , these assignment operations in string are too costly , which causes TLE.
But isn't to_string() O(1)?
$$$ s = s + $$$ thing is $$$ O({n^2}) $$$ as far as I know.
i think , this should help https://www.youtube.com/watch?v=meGcKZYxVe8&list=PLauivoElc3ghk5480uu-T5nkEUY6Tbm6V&index=4
s=s+to_string, is itself an operation in O(length of string), as it first creates a copy of itself, and then appends another string to it, which results to TLE
try to avoid using strings, this problem can be solved easily without really maintaining any additional $$$O(n)$$$ structure, in your for loop you are reversing a string which could grow linearly, so your code is essentially $$$O(n^2)$$$
I am not reversing the string inside the loop though but yeah i get your point-will avoid string operations
okay, I didnt see it, but still '+=' operator can take as much as length of the new string time, look at cpp docs here