Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word
Given a string str. The task is to count the words having the same length as str and each letter at the i-th position is either (i-1)-th, i-th, or (i+1)-th position letter of str.
Note: For the first letter consider i-th and (i+1)-th position letter of W. And for last letter consider (i-1)-th and i-th position letter of str.
Examples:
Input : str[] = "ab"
Output : 4
Words that can be formed: aa, ab, ba, bb.
Input : str[] = "x"
Output : 1
For any letter at index i, except first and last letter, there are three possible letter i.e (i-1)th, ith or (i+1)th letter of given words. So, if three of them are distinct, we have 3 possibilities. If two of them are same, we have 2 possibilities. And if all are same we have only 1 possibility.
So, traverse the given words and find the possibility of each letter and multiply them.
Similarly, for first letter check the distinct letter at first and second position. And for last position check the distinct letter at last and second last position.
Below is the implementation of this approach:
C++
#include<bits/stdc++.h>
using namespace std;
int countWords( char str[], int len)
{
int count = 1;
if (len == 1)
return count;
if (str[0] == str[1])
count *= 1;
else
count *= 2;
for ( int j=1; j<len-1; j++)
{
if (str[j] == str[j-1] && str[j] == str[j+1])
count *= 1;
else if (str[j] == str[j-1] ||
str[j] == str[j+1] ||
str[j-1] == str[j+1])
count *= 2;
else
count *= 3;
}
if (str[len - 1] == str[len - 2])
count *= 1;
else
count *= 2;
return count;
}
int main()
{
char str[] = "abc" ;
int len = strlen (str);
cout << countWords(str, len) << endl;
return 0;
}
|
Java
public class GFG {
static int countWords(String str, int len)
{
int count = 1 ;
if (len == 1 )
return count;
if (str.charAt( 0 ) == str.charAt( 1 ))
count *= 1 ;
else
count *= 2 ;
for ( int j = 1 ; j < len - 1 ; j++)
{
if (str.charAt(j) == str.charAt(j - 1 ) &&
str.charAt(j) == str.charAt(j + 1 ))
count *= 1 ;
else if (str.charAt(j) == str.charAt(j - 1 )||
str.charAt(j) == str.charAt(j + 1 ) ||
str.charAt(j - 1 ) == str.charAt(j + 1 ))
count *= 2 ;
else
count *= 3 ;
}
if (str.charAt(len - 1 ) == str.charAt(len - 2 ))
count *= 1 ;
else
count *= 2 ;
return count;
}
public static void main(String args[])
{
String str = "abc" ;
int len = str.length();
System.out.println(countWords(str, len));
}
}
|
Python 3
def countWords( str , l):
count = 1 ;
if (l = = 1 ):
return count
if ( str [ 0 ] = = str [ 1 ]):
count * = 1
else :
count * = 2
for j in range ( 1 ,l - 1 ):
if ( str [j] = = str [j - 1 ] and str [j] = = str [j + 1 ]):
count * = 1
else if ( str [j] = = str [j - 1 ] or
str [j] = = str [j + 1 ] or
str [j - 1 ] = = str [j + 1 ]):
count * = 2
else :
count * = 3
if ( str [l - 1 ] = = str [l - 2 ]):
count * = 1
else :
count * = 2
return count
if __name__ = = "__main__" :
str = "abc"
l = len ( str )
print (countWords( str , l))
|
C#
using System;
public class GFG {
static int countWords( string str, int len)
{
int count = 1;
if (len == 1)
return count;
if (str[0] == str[1])
count *= 1;
else
count *= 2;
for ( int j = 1; j < len - 1; j++) {
if (str[j] == str[j - 1] &&
str[j] == str[j + 1])
count *= 1;
else if (str[j] == str[j - 1] ||
str[j] == str[j + 1] ||
str[j - 1] == str[j + 1])
count *= 2;
else
count *= 3;
}
if (str[len - 1] == str[len - 2])
count *= 1;
else
count *= 2;
return count;
}
public static void Main()
{
string str = "abc" ;
int len = str.Length;
Console.WriteLine(countWords(str, len));
}
}
|
PHP
<?php
function countWords( $str , $len )
{
$count = 1;
if ( $len == 1)
return $count ;
if ( $str [0] == $str [1])
$count *= 1;
else
$count *= 2;
for ( $j = 1; $j < $len - 1; $j ++)
{
if ( $str [ $j ] == $str [ $j - 1] &&
$str [ $j ] == $str [ $j + 1])
$count *= 1;
else if ( $str [ $j ] == $str [ $j - 1] ||
$str [ $j ] == $str [ $j + 1] ||
$str [ $j - 1] == $str [ $j + 1])
$count *= 2;
else
$count *= 3;
}
if ( $str [ $len - 1] == $str [ $len - 2])
$count *= 1;
else
$count *= 2;
return $count ;
}
$str = "abc" ;
$len = strlen ( $str );
echo countWords( $str , $len ) ;
?>
|
Javascript
<script>
function countWords(str,len)
{
let count = 1;
if (len == 1)
return count;
if (str[0] == str[1])
count *= 1;
else
count *= 2;
for (let j = 1; j < len - 1; j++)
{
if (str[j] == str[j-1] &&
str[j] == str[j+1])
count *= 1;
else if (str[j] == str[j-1]||
str[j] == str[j+1] ||
str[j-1] == str[j+1])
count *= 2;
else
count *= 3;
}
if (str[len-1] == str[len-2])
count *= 1;
else
count *= 2;
return count;
}
let str = "abc" ;
let len= str.length;
document.write(countWords(str, len));
</script>
|
Time complexity : O(n) where n is length of string.
Auxiliary Space: O(1)
Last Updated :
13 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...