Made Equal Strings by Increasing Prefixes

Robert is expert in strings where he challenges everyone to write a program for the below implementation.
Two strings and comprising of lower case English letters are compatible if they are equal or can be made equal by following this step any number of times:
Select a prefix from the string (possibly empty), and increase the alphabetical value of all the characters in the prefix by the same valid amount.
For example if the string is abc and we select the prefix ab then we can convert it to bcc by increasing the alphabetical value by 1. But if we select the prefix abc then we cannot increase the alphabetical value.
Your task is to determine if given strings are compatible.
Sample Input
abaca
cdbda
Sample Output
Yes

#include<stdio.h>
#include<string.h>
int main() {
  char str1[30],str2[30];
  int i,j,k,lens[30];
  fgets(str1,30,stdin);
  fgets(str2,30,stdin);
  if(strlen(str1)!=strlen(str2)){
    printf("No");
  }else{
    for(i=0;i<strlen(str1);i++){
      if(str2[i]>=str1[i])
        lens[i] = str2[i] - str1[i];
      else{
        printf("No");
        break;
      }
    }
    if(i==strlen(str1)){
      for(i=0;i<strlen(str1)-1;i++){
        if(lens[i]<lens[i+1]){
          printf("No");
          break;
        }
      }
      if(i==strlen(str1)-1)
        printf("Yes");
    }
  }
  return 0;
}

No comments:

Post a Comment

Total Pageviews