Funny String – HackerRank Solution

In this post, we will solve Funny String HackerRank Solution. This problem (Funny String) is a part of HackerRank Problem Solving series.

Task

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]sreverse = ‘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

Explanation

Let r be the reverse of s.

Test Case 0:
s = acxz, r = zxca
Corresponding ASCII values of characters of the strings:
s = [97. 99, 120, 122] and r = [122, 120, 99, 97]
For both the strings the adjacent difference list is [2, 21, 2].

Test Case 1:
s = bcxz, r = zxcb
Corresponding ASCII values of characters of the strings:
s = [98, 99, 120, 122] and r = [122, 120, 99, 98]
The difference list for string s is [1, 21, 2] and for string r is [2, 21, 1].

Solution – Funny String – HackerRank Solution

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() { 
    int N;
    cin>>N;
    for(int i = 0; i < N; i++) {
        string s;
        cin>>s;
        bool funny = true;
        // Only need to iterate over half the string
        for(int j = 0; j < s.length()/2; j++) {
            int k;
            k = s.length()-j-1;
            // We don't need to cast chars to ints, using the "-" operator automatically converts chars to their ascii values
            if(abs((s[k] - s[k-1])) != abs((s[j] - s[j+1]))) {
                cout<<"Not Funny"<<"\n";
                funny = false;
                break;
            }
        }
        if(funny) {
            cout<<"Funny"<<"\n";
        }
    }
    return 0;
}

Python

import sys
import string

alpha = string.ascii_lowercase

def funnyString(s):
    res = 'Funny'
    r = s[::-1]
    
    for ind in range(1, len(s)):
        #print("{} - {} ?= {} - {}".format(alpha.index(s[ind]), alpha.index(s[ind-1]), alpha.index(r[ind]), alpha.index(r[ind-1])))
        if abs(alpha.index(s[ind]) - alpha.index(s[ind-1])) != abs(alpha.index(r[ind]) - alpha.index(r[ind-1])):
            res = 'Not Funny'
            break
            
    return res

q = int(input().strip())
for a0 in range(q):
    s = input().strip()
    result = funnyString(s)
    print(result)

Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static String funnyString(String s){
        String r = (new StringBuffer(s)).reverse().toString();
        for (int i = 1; i < s.length(); i++) {
            if (Math.abs(((int)s.charAt(i)) - ((int)s.charAt(i-1))) != Math.abs(((int)r.charAt(i)) - ((int)r.charAt(i-1)))) {
                return "Not Funny";
            }
        }
        return "Funny";
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            String s = in.next();
            String result = funnyString(s);
            System.out.println(result);
        }
    }
}

Note: This problem (Funny String) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *