HackerRank Funny String Problem Solution
In this post, we will solve HackerRank Funny String Problem Solution.
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
orNot Funny
Input Format
The first line contains an integer q, the number of queries.
The next q lines each contain a string, s.
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 $.
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 $ is [1, 21, 2] and for string r is [2, 21, 1].
Funny String C Solution
#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;
}
Funny String C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int t;
std::cin >> t;
while(t--) {
std::string S;
std::cin >> S;
int n = S.size();
bool isfunny=true;
for(int i=1;i<n;++i) {
if(std::abs(int(S[i])-int(S[i-1])) !=
std::abs(int(S[n-1-i])-int(S[n-i]))) {
isfunny=false;
}
}
std::cout << (isfunny ? "Funny" : "Not Funny") << std::endl;
}
return 0;
}
Funny String C Solution
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
private static void Main(string[] args)
{
int T = int.Parse(Console.ReadLine());
for (int i = 0; i < T; i++)
{
string s = Console.ReadLine();
string result = isFunny(s);
Console.WriteLine(result);
}
}
private static string isFunny(string s)
{
for (int i = 0, j = s.Length - 1; i < s.Length - 1; i++, j--)
{
if (Math.Abs(s[i + 1] - s[i]) != (Math.Abs(s[j - 1] - s[j])))
{
return "Not Funny";
}
}
return "Funny";
}
}
Funny String Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'funnyString' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
public static String funnyString(String s) {
// Write your code here
String reversedString = new StringBuilder().append(s).reverse().toString();
char[] sCharArray = s.toCharArray();
char[] reversedCharArray = reversedString.toCharArray();
for (int i = 0; i < sCharArray.length - 1; i++) {
int temp = Math.abs((sCharArray[i]) - sCharArray[i+1]);
int temp2 = Math.abs(reversedCharArray[i] - reversedCharArray[i+1]);
if (temp != temp2) {
return "Not Funny";
}
}
return "Funny";
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int q = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, q).forEach(qItr -> {
try {
String s = bufferedReader.readLine();
String result = Result.funnyString(s);
bufferedWriter.write(result);
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}
Funny String JavaScript Solution
String.prototype.charCode = function () {
code = this[0].charCodeAt(0);
return code;
}
function processData(input) {
lines = input.split('\n');
count = parseInt(lines[0]);
tests = getTests(lines, count);
tests.forEach(function(word, index, array) {
funny = null;
S = word.split('');
R = S.slice(0).reverse();
for (i = 1; i < S.length; i++) {
Si = S[i].charCode();
prevSi = S[i-1].charCode();
Ri = R[i].charCode();
prevRi = R[i-1].charCode();
isFunnyCharSet = Math.abs(Si - prevSi) === Math.abs(Ri - prevRi);
if (!isFunnyCharSet) {
funny = false;
}
}
if (funny === false) {
console.log("Not Funny");
} else {
console.log("Funny");
}
})
}
function getTests(lines, count) {
tests = []
for (i = 1; i <= count; i++) {
tests.push(lines[i]);
}
return tests;
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Funny String Python Solution
from sys import stdin
T = int(stdin.readline())
for _ in range(T):
s = list(map(ord, stdin.readline().strip()))
r = s[::-1]
sdif = [abs(s[i] - s[i-1]) for i in range(1, len(s))]
rdif = [abs(r[i] - r[i-1]) for i in range(1, len(s))]
print('Funny' if sdif == rdif else 'Not Funny')
Other Solutions