Funny String

Funny String

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. . 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

1
2
3
4
5
STDIN   Function
----- --------
2 q = 2
acxz s = 'acxz'
bcxz s = 'bcxz'

Sample Output

1
2
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:

[97, 99, 120, 122] and [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 is [2, 21, 1].


Solution

Solution 1 with JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* Complete the 'funnyString' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/

function funnyString(s) {
// Write your code here
const values = new Array(s.length).fill(0).reduce((target, item, index) => {
target.push(s.charCodeAt(index));

return target;
}, []);

let count = values.length - 1;

for (let i = 0, itotal = values.length - 1; i < itotal; i++) {
let a = Math.abs(values[i] - values[i + 1]);
let b = Math.abs(values[count - i] - values[count - i - 1]);

if (a !== b) {
return 'Not Funny';
}
}

return 'Funny';
}

Solution 2 with TS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
* Complete the 'funnyString' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
function funnyString(s: string): string {
// Write your code here
const values = new Array(s.length)
.fill(0)
.reduce((target: number[], _, index: number) => {
target.push(s.charCodeAt(index));

return target;
}, [] as number[]);

let count: number = values.length - 1;

for (let i: number = 0, itotal: number = values.length - 1; i < itotal; i++) {
let a: number = Math.abs(values[i] - values[i + 1]);
let b: number = Math.abs(values[count - i] - values[count - i - 1]);

if (a !== b) {
return 'Not Funny';
}
}

return 'Funny';
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×