We are given a sentence. Our task is to print all funny words/strings in that sentence.
What is a funny word ?
Reverse the given string. Iterate through each character of that string, compare the absolute difference in the ASCII values of the characters at positions 0 and 1, 1 and 2, 2 and 3 and so on to the end. If the list of absolute differences is the same for both strings, they are funny otherwise not.
Examples:
Input : HKMNPS
Output : HKMNPS
Explanation:
Let r be the reverse of original string s
s = “HKMNPS”
r = “SPNMKH”
|H-K| = 3 = |S-P|
|K-M| = 2 = |P-N|
|M-N| = 1 = |N-M|
|N-P| = 2 = |M-K|
|P-S| = 3 = |K-H|
Since each comparison is equal so given string is funny
Input : bdwy
Output : bdwy
NOTE: Every palindrome string is a funny string but not vice-versa.
The idea is to split the string into words. For every word, traverse it from both ends and compare differences between adjacent characters.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkFunny(string word)
{
int i = 1;
int j = word.length() - 2;
for ( int i = 0; i < word.length(); i++)
word[i] = tolower (word[i]);
while (i <= j) {
if ( abs (word[i] - word[i - 1])
!= abs (word[j] - word[j + 1]))
return false ;
i++;
j--;
}
return true ;
}
void printFunnyWords(string str)
{
str += " " ;
string word = "" ;
for ( int i = 0; i < str.length(); i++) {
char ch = str[i];
if (ch != ' ' )
word += ch;
else {
if (checkFunny(word))
cout << word << endl;
word = "" ;
}
}
if (word != "" ) {
if (checkFunny(word))
cout << word << endl;
}
}
int main()
{
printFunnyWords(
"Miss Arora teaches us malayalam bdwy" );
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class Funny {
static boolean checkFunny(String word)
{
int i = 1 ;
int j = word.length() - 2 ;
word = word.toLowerCase();
while (i <= j) {
if ((Math.abs(word.charAt(i) - word.charAt(i - 1 ))) !=
Math.abs((word.charAt(j) - word.charAt(j + 1 ))))
return false ;
i++;
j--;
}
return true ;
}
static void printFunnyWords(String str)
{
str = str + " " ;
String word = "" ;
for ( int i = 0 ; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch != ' ' )
word = word + ch;
else {
if (Funny.checkFunny(word))
System.out.println(word);
word = "" ;
}
}
}
public static void main(String[] args)
{
Funny.printFunnyWords( "Miss Arora teaches us malayalam bdwy " );
}
}
|
Python3
def checkFunny(word):
i = 1
j = len (word) - 2
word = word.lower()
while (i < = j):
if (( abs ( ord (word[i]) - ord (word[i - 1 ])))
! = abs (( ord (word[j]) - ord (word[j + 1 ])))):
return False
i = i + 1
j = j - 1
return True
def printFunnyWords( str ):
str = str + " "
word = ""
i = 0
for i in range ( len ( str )):
ch = str [i]
if (ch ! = ' ' ):
word = word + ch
else :
if (checkFunny(word)):
print (word)
word = ""
printFunnyWords( "Miss Arora teaches us malayalam bdwy " )
|
C#
using System;
class GFG
{
public static bool checkFunny( string word)
{
int i = 1;
int j = word.Length - 2;
word = word.ToLower();
while (i <= j)
{
if ((Math.Abs(word[i] -
word[i - 1])) != Math.Abs((word[j] -
word[j + 1])))
{
return false ;
}
i++;
j--;
}
return true ;
}
public static void printFunnyWords( string str)
{
str = str + " " ;
string word = "" ;
for ( int i = 0; i < str.Length; i++)
{
char ch = str[i];
if (ch != ' ' )
{
word = word + ch;
}
else
{
if (GFG.checkFunny(word))
{
Console.WriteLine(word);
}
word = "" ;
}
}
}
public static void Main( string [] args)
{
GFG.printFunnyWords( "Miss Arora teaches us " +
"malayalam bdwy " );
}
}
|
Javascript
<script>
function checkFunny(word)
{
var i = 1;
var j = word.length - 2;
word= (word.toLowerCase());
while (i <= j)
{
if (Math.abs(word[i].charCodeAt(0) -
word[i - 1].charCodeAt(0)) !=
Math.abs(word[j].charCodeAt(0) -
word[j + 1].charCodeAt(0)))
return false ;
i++;
j--;
}
return true ;
}
function printFunnyWords(str)
{
str += " " ;
var word = "" ;
for ( var i = 0; i < str.length; i++)
{
var ch = str[i];
if (ch != ' ' )
word += ch;
else
{
if (checkFunny(word))
document.write( word + "<br>" );
word = "" ;
}
}
}
printFunnyWords( "Miss Arora teaches us malayalam bdwy " );
</script>
|
OutputArora
us
malayalam
bdwy
Time Complexity: O(|s|)
Auxiliary Space: O(|s|), where |s| is the length of the string.