In this HackerRank Funny String, problem Determine whether a given string is funny. If it is, return Funny, otherwise, return Not Funny.

HackerRank Funny String problem solution


Problem solution in Python programming.

import sys

strings = []
for line in sys.stdin:
    strings.append(line.strip())

for i in strings[1:]:
    S = i
    R = i[::-1]
    funny = True
    for j in range(1, len(i)):
        if abs(ord(S[j]) - ord(S[j-1])) != abs(ord(R[j]) - ord(R[j-1])):
            funny = False
            break
            
    if funny:
        print("Funny")
    else:
        print("Not Funny")


Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int test_cases = sc.nextInt();
        Solution p = new Solution();
        for (int i = 0; i < test_cases; i++) {
            String str = sc.next();
            p.isFunny(str);
        }
    }

     void isFunny(String str) {
        int str_len = str.length();

        int i = 1, j = str_len -1;
        boolean flagFound = false;
        while (i < str_len && j > 0) {
            int diff = Math.abs((int) str.charAt(i) - str.charAt(i - 1));
            int rev_diff = Math.abs( (int) str.charAt(j) - str.charAt(j - 1) );
            if(rev_diff != diff) {
                flagFound = true;
                break;
            }

            i++;
            j--;
        }

        if(flagFound) System.out.println("Not Funny");
        else System.out.println("Funny");
    }
}


Problem solution in C++ programming.

#include <iostream>
#include <string>
using namespace std;

string rev(string s) {
    string r;
    for(int i = 0; i < s.length(); i ++)
        r = string("") + s[i] + r;
    return r;
}

bool funny() {
	string s;
	cin >> s;
	string r = rev(s);
	//reverse(r.begin(), r.end());
	for(int i = 1; i < s.length(); i ++)
		if(abs(s[i] - s[i - 1]) != abs(r[i] - r[i - 1])) return false;
	return true;
}

int main() {
	int t;
	cin >> t;
	while(t --) {
		if(funny()) cout << "Funny" << endl;
		else cout << "Not Funny" << endl;
	}
	return 0;
}


Problem solution in C programming.

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

int main() {
    char str_buf[10000] = {0};
    int T;
    scanf("%d", &T);
    for(int i = 0; i < T; i++) {
        scanf("%s", str_buf);
        int is_funny = 1;
        int str_len = strlen(str_buf);
        for(int j = 1; j < str_len; j++) {
            int sdif = str_buf[j]-str_buf[j-1];
            int rdif = str_buf[str_len-1-j]-str_buf[str_len-j];
            if(abs(sdif) != abs(rdif)) {
                is_funny = 0;
                break;
            }
        }
        if(is_funny){
            printf("Funny\n");
        } else {
            printf("Not Funny\n");
        }
        memset(str_buf, 0, 10000*sizeof(char));
    }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}


Problem solution in JavaScript programming.

function isFunny(string){
    string = string.map(function(letter){
        return letter.charCodeAt(0);
    });
    
    for(var i = 1; i < string.length; i++){
        var S = Math.abs(string[i]-string[i-1]);
        var j = string.length - i;
        var R = Math.abs(string[j-1]-string[j]);
        if (R !== S){
            return 'Not Funny';
        }
    }
    return 'Funny';
}

function processData(input) {
    var data = input.split('\n').slice(1).map(function(line){ return line.split('')});
    for(l in data){
        console.log(isFunny(data[l]));
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});