# Funny String HackerRank Solution in C, C++, Java, Python

In this challenge, you will determine whether a string is funny or not. To determine whether a string is funny, create a copy of the string in reverse e.g.abc->cba . Iterating through each string, compare the absolute difference in the ascii values of the characters at positions 0 and 1, 1 and 2 and so on to the end. If the list of absolute differences is the same for both strings, they are funny.

Determine whether a give string is funny. If it is, return Funny, otherwise return Not Funny.

Example

s=’lmnop’

The ordinal values of the charcters are [108,109,110,111,112]. S reverse=’ponml’ and the ordinals are [112,111,110,109,108]. The absolute differences of the adjacent elements for both strings are [1,1,1,1], so the answer is Funny.

Function Description

Complete the funnyString function in the editor below.

funnyString has the following parameter(s):

• string s: a string to test

Returns

• string: either Funny or Not Funny

Input Format

The first line contains an integer q, the number of queries.

The next q lines each contain a string,s .

Constraints

• 1<=q<=10
• 2<=length of s<=10000

Sample Input

```STDIN   Function

-----   --------

2       q = 2

acxz    s = 'acxz'

bcxz    s = 'bcxz'```

Sample Output

```Funny

Not Funny```

## Funny String HackerRank Solution in C

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

#define MAX(_n, _m) ((_n > _m)?_n:_m)
#define MIN(_n, _m) ((_n < _m)?_n:_m)

int main() {

int  testcnt    = 0;
char str[10001] = {0};
int  len        = 0;

scanf("%d", &testcnt);

for (int test = 0; test < testcnt; test++){
scanf("%s", str);
len = strlen(str);

int funny = 1;
for (int i = 1, j = len - 1; i < len; i++, j--){
if (MAX(str[i], str[i-1]) - MIN(str[i], str[i-1]) !=
MAX(str[j], str[j-1]) - MIN(str[j], str[j-1])){
funny = 0;
break;
}
}

if (funny){
printf("Funny\n");
}
else{
printf("Not Funny\n");
}

}

return 0;
}```

## Funny String HackerRank Solution in C++

```#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
#include<functional>
#include<climits>
#include<cassert>
#include<iterator>
using namespace std;
string s;
int t;
char ss[1000001];
int main(){
scanf("%d", &t);
while (t--){
scanf("%s", ss);
s = ss;
//cin >> s;
string r = s;
bool ok = false;
reverse(r.begin(), r.end());
for (int i = 0; i < s.size() - 1; i++){
if (abs(s[i] - s[i + 1]) != abs(r[i] - r[i + 1])){
ok = true;
break;
}
}
if (ok){
puts("Not Funny");
}
else{
puts("Funny");
}
}
return 0;
}```

## Funny String HackerRank Solution in Java

```import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class FS {
static InputStream is;
static PrintWriter out;
static String INPUT = "";

static void solve()
{
outer:
for(int T = ni();T >= 1;T--){
char[] s = ns().toCharArray();
int n = s.length;
for(int i = 0;i < n-1;i++){
if(Math.abs(s[i+1]-s[i]) == Math.abs(s[n-1-i] - s[n-1-i-1])){
}else{
out.println("Not Funny");
continue outer;
}
}
out.println("Funny");
}
}

public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);

solve();
out.flush();
long G = System.currentTimeMillis();
tr(G-S+"ms");
}

private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;

try {
is.mark(1000);
while(true){
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}

private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;

{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}

private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

private static double nd() { return Double.parseDouble(ns()); }
private static char nc() { return (char)skip(); }

private static String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
}
return sb.toString();
}

private static char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
}
return n == p ? buf : Arrays.copyOf(buf, p);
}

private static char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}

private static int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}

private static int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
}

while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
}

private static long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
}

while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
}

private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}```

## Funny String HackerRank Solution in Python

```# Enter your code here. Read input from STDIN. Print output to STDOUT

import fileinput

line = fileinput.input()
n = int(line[0])
for i in range(n):
s = line[i+1].strip()
lst = [ord(c) for c in s]
nlst = lst[:]
nlst.reverse()

ln = len(s)
funny = True
for k in range(1, ln):
if abs(lst[k]-lst[k-1]) != abs(nlst[k]-nlst[k-1]):
funny = False
break

if funny: print "Funny"
else: print "Not Funny"

```

## Funny String HackerRank Solution in C#

```using System;

namespace FunnyString
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < t; i++)
{
HandleTestCase();
}
}

static void HandleTestCase()
{
for (int i = 0; i < str.Length - 1; i++)
{
int lastIndex = str.Length - i - 1;
int mod1 = Math.Abs(str[i] - str[i + 1]);
int mod2 = Math.Abs(str[lastIndex] - str[lastIndex - 1]);

if (mod1 != mod2)
{
Console.WriteLine("Not Funny");
return;
}
}

Console.WriteLine("Funny");
}
}
}```

Attempt Funny String HackerRank Challenge

Next HackerRank Challenge Solution