Funny String Hackerrank problem solution

Funny String Hackerrank

Suppose you have a String, S, of length N that is indexed from 0 to N-1. You also have some String, R, that is the reverse of String S. S is funny if the condition S[i] – S[i-1] = R[i] – R[i-1] is true for every character from 1 to N-1.
Note: For some String S, S[i] denotes the ASCII value of the ith 0-indexed character in S. The absolute value of an integer, x, is written as |x|.

Input Format

The first line contains an integer, T (the number of test cases).
Each line i of the T subsequent lines contain a string, S.

Constraints
1<=T<=10 0<=i<=T-1 2<=length of S<=10000 Output Format

For each String Sj (where 0<=j<=T-1), print whether it is Funny or Not Funny on a new line. Sample Input

2
acxz
bcxz

Sample Output

Funny
Not Funny

Funny String Hackerrank

Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#define MAX_STR 10001

int main() {
    char arr[MAX_STR];
    int t;
    scanf("%d", &t);

    for (; t-- > 0;) {
        int not_funny;
        scanf("%s", arr);
        int len = strlen(arr);
        for (int front1 = 0, front2= 1, end1 = len - 1, end2 = len - 2; --len > 0;) {
            not_funny =  (abs(arr[front2++] - arr[front1++]) ^ abs(arr[end2--] - arr[end1--]));
            if (not_funny)
                break;
        }
        printf("%s\n", (not_funny) ? "Not Funny" : "Funny");
    }
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>

int main() {
    int t;
    char s[10000],revstr[10000];
    scanf("%d",&t);
    for(int k=0; k<t; k++){
        scanf("%s", s);
        int x=strlen(s);
       
 // Reversing string
 int j=0;
 for(int i=x-1;i>=0;i--)
 {
  revstr[j]=s[i];
  j++;
 }
 revstr[j]='\0';
int c=0;
        for(int l=0; l<x;l++){
           if( abs(revstr[l]-revstr[l-1])==abs(s[l]-s[l-1]))
               c++;               
        }
        if(c==x-1)
            printf("Funny");
        else
            printf("Not Funny");
        printf("\n");
    }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}
for _ in range(input()):
    s = list(raw_input())
    r = s[::-1]
    for i in range(len(s)-1):
        if abs(ord(s[i]) - ord(s[i-1])) != abs(ord(r[i]) - ord(r[i-1])):
            print "Not Funny"
            break
    else:
        print "Funny"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;
typedef  long long i64;
typedef unsigned long long ui64;

#define MOD 10000000

string chk(string s){
	int n = s.size();
	string r = s;
	reverse(r.begin(),r.end());
	for(int i=1;i<n;i++){
		int d1=s[i]-s[i-1];
		int d2=r[i]-r[i-1];
		if(abs(d1)!=abs(d2))return "Not Funny";
	}
	return "Funny";
}
string isFunny(string s){
	return chk(s);
}
int main(){

	string s1;
	int t;
	cin>>t;
	while(t--){
		cin>>s1;
		cout<<isFunny(s1)<<endl;
	}
}

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.