HACKER RANK Algorithm Strings Challenges - Funny String Solution.'C'

Algorithm Strings Challenges - Funny String Solution
Problem Statement
Suppose you have a string S which has length N and is indexed from 0 to N1. String R is the reverse of the string S. The string S is funny if the condition |SiSi1|=|RiRi1|is true for every i from 1 to N1.
(Note: Given a string strstri denotes the ascii value of the ith character (0-indexed) of str. |x| denotes the absolute value of an integer x)
Input Format
First line of the input will contain an integer TT testcases follow. Each of the next T lines contains one string S.
Constraints
  • 1<=T<=10
  • 2<=length of S<=10000
Output Format
For each string, print Funny or Not Funny in separate lines.
Sample Input
2
acxz
bcxz
Sample Output
Funny
Not Funny
Explanation
Consider the 1st testcase acxz
here
|c-a| = |x-z| = 2
|x-c| = |c-x| = 21
|z-x| = |a-c| = 2
Hence Funny.
Consider the 2nd testcase bcxz
here
|c-b| != |x-z|
Hence Not Funny.

Solution:

#include <cmath>
#include <cstdio>
#include<cstring>
using namespace std;
int main() {
    int t;
    char s[10001];
    scanf("%d",&t);
    while(t--)
        {
        scanf("%s",s);
        int i=0,j=strlen(s);
        int d=1;
        for(i=0;i<j-1;i++)
            {
           if(!(abs(s[i+1]-s[i])==abs(s[j-i-1]-s[j-i-2])))
               {
                    d=0;
                   break;
               }         
        }
        if(d==0)
            {
            printf("Not Funny\n");
        }
        else
            {
            printf("Funny\n");
        }          
     }
    return 0;
}

Thanks for Visiting, Hope this helps you....

Copyright © 2015 HackerRank.
All Rights Reserved

Comments

Popular Posts