Given a string str and the task is to count palindrome words present in the string str.
Examples:
Input : Madam Arora teaches malayalam
Output : 3
The string contains three palindrome words (i.e.,
Madam, Arora, malayalam) so the count is three.
Input : Nitin speaks malayalam
Output : 2
The string contains two palindrome words (i.e.,
Nitin, malayalam) so the count is two.
Method 1:
countPalin() function counts the number of palindrome words by extracting every word of the string and passing it to checkPalin() function. An extra space is added in the original string to extract last word.
checkPalin() function check the word palindrome. It returns 1 if word is palindrome else returns 0. It makes sure that empty strings are not counted as palindrome as the user may enter more than one spaces in between or at the beginning of the string.
C++
#include <bits/stdc++.h>
using namespace std;
bool checkPalin(string word)
{
int n = word.length();
transform(word.begin(), word.end(), word.begin(), :: tolower );
for ( int i = 0; i < n; i++,n--)
if (word.at(i) != word.at(n - 1))
return false ;
return true ;
}
int countPalin(string str)
{
str = str + " " ;
string word = "" ;
int count = 0;
for ( int i = 0; i < str.length(); i++)
{
char ch = str.at(i);
if (ch != ' ' )
word = word + ch;
else {
if (checkPalin(word))
count++;
word = "" ;
}
}
return count;
}
int main()
{
cout<<countPalin( "Madam Arora teaches malayalam" )<<endl;
cout<<countPalin( "Nitin speaks malayalam" )<<endl;
}
|
Java
class GFG {
static boolean checkPalin(String word)
{
int n = word.length();
word = word.toLowerCase();
for ( int i= 0 ; i<n; i++,n--)
if (word.charAt(i) != word.charAt(n - 1 ))
return false ;
return true ;
}
static int countPalin(String str)
{
str = str + " " ;
String word = "" ;
int count = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
char ch = str.charAt(i);
if (ch != ' ' )
word = word + ch;
else {
if (checkPalin(word))
count++;
word = "" ;
}
}
return count;
}
public static void main(String args[])
{
System.out.println(countPalin( "Madam "
+ "Arora teaches malayalam" ));
System.out.println(countPalin( "Nitin "
+ "speaks malayalam" ));
}
}
|
Python3
def checkPalin(word):
if word.lower() = = word.lower()[:: - 1 ]:
return True
def countPalin( str ):
count = 0
listOfWords = str .split( " " )
for elements in listOfWords:
if (checkPalin(elements)):
count + = 1
print (count)
countPalin( "Madam Arora teaches malayalam" )
countPalin( "Nitin speaks malayalam" )
|
C#
using System;
class GFG
{
public static bool checkPalin( string word)
{
int n = word.Length;
word = word.ToLower();
for ( int i = 0; i < n; i++,n--)
{
if (word[i] != word[n - 1])
{
return false ;
}
}
return true ;
}
public static int countPalin( string str)
{
str = str + " " ;
string word = "" ;
int count = 0;
for ( int i = 0; i < str.Length; i++)
{
char ch = str[i];
if (ch != ' ' )
{
word = word + ch;
}
else
{
if (checkPalin(word))
{
count++;
}
word = "" ;
}
}
return count;
}
public static void Main( string [] args)
{
Console.WriteLine(countPalin( "Madam " +
"Arora teaches malayalam" ));
Console.WriteLine(countPalin( "Nitin " +
"speaks malayalam" ));
}
}
|
Javascript
<script>
function checkPalin(word)
{
var n = word.length
word = word.toLowerCase();
for ( var i = 0; i < n; i++,n--)
if (word[i] != word[n - 1])
return false ;
return true ;
}
function countPalin( str)
{
str = str + " " ;
var word = "" ;
var count = 0;
for ( var i = 0; i < str.length; i++)
{
var ch = str[i];
if (ch != ' ' )
word = word + ch;
else {
if (checkPalin(word))
count++;
word = "" ;
}
}
return count;
}
document.write( countPalin( "Madam Arora teaches malayalam" ) + "<br>" );
document.write( countPalin( "Nitin speaks malayalam" ));
</script>
|
Time Complexity: O(n2), where n is the size of the given string.
Auxiliary Space: O(n), where n is the size of the given string.
https://youtu.be/z
-5HChTA0ME