Easy brute force solution in cpp


#1
int sanu(char A){
    if(A=='I') return 1;
    if(A=='V') return 5;
    if(A=='X') return 10;
    if(A=='L') return 50;
    if(A=='C') return 100;
    if(A=='D') return 500;
    if(A=='M') return 1000;
    
}
int Solution::romanToInt(string A) {
   
  int n=A.size();
  int sum=0;
  if(n==0) return 0;
  sum+=sanu(A[n-1]);
  for(int i=n-2; i>=0; i--){
      
      if(A[i]=='I'&&(A[i+1]=='X'||A[i+1]=='V')){
          sum-=1;
      }
       else if(A[i]=='X'&&(A[i+1]=='C'||A[i+1]=='L')){
          sum-=10;
      }
       else if(A[i]=='C'&&(A[i+1]=='D'||A[i+1]=='M')){
          sum-=100;
      }
      else sum+=sanu(A[i]);
      
  }return sum;

}