I have taken of all border cases like:
- string of all zeroes
- no starting redundant zeroes in output
- giving correct ans for all the test cases I could think of
Here’s my code:
#include<string>
#include<iostream>
#include<cstdio>
using namespace std;
bool isbigger(string a,string b,int j) // returns true if auxillary number > max
{
int asz=j+1,bsz=b.size(),diff=bsz-asz; //asz is size of a, sim bsz
if(diff>0) while(diff--) a='0'+a;//padding0
else if(diff<0){
diff=-diff;
while(diff--) b='0'+b; //padding 0
}
if(a>b) return 1;
else return 0;
}
main(){
string a;
cin>>a;
int i,j,k,chars=0;
string aux="",max="0";
for(i=0;a[i]=='0';i++);
for(;a[i];i++){
aux="";
for(j=0;a[i+j]=='0';j++);
for(;a[i+j];j++){
if(a[i+j]>='A' && a[i+j]<='Z'){
chars++;
if(chars>1) break;
aux+='9';}
else aux+=a[i+j];
if(isbigger(aux,max,j)) max=aux;
}
chars=0;
}
if(max=="") max="0";
cout<<max;
}//end of code