Sunday, 21 January 2018

Funny String Hacker Rank Solution in C

Problem:
https://www.hackerrank.com/challenges/funny-string/problem

Solution:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

char* funnyString(char* s){
    // Complete this function
    int len=strlen(s)-1;
    for(int i=0;i<=len;i=i+2,len=len-2)
    {
        if(abs(s[i]-s[i+1]) != abs(s[len]-s[len-1]))
               return "Not Funny";
    }
    return "Funny";
}

int main() {
    int q; 
    scanf("%i", &q);
    for(int a0 = 0; a0 < q; a0++){
        char* s = (char *)malloc(512000 * sizeof(char));
        scanf("%s", s);
        int result_size;
        char* result = funnyString(s);
        printf("%s\n", result);
    }
    return 0;
}