# 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

 `// C++ program to count words  whose ith letter ` `// is either (i-1)th, ith, or (i+1)th letter ` `// of given word. ` `#include ` `using` `namespace` `std; ` ` `  `// Return the count of words. ` `int` `countWords(``char` `str[], ``int` `len) ` `{ ` `    ``int` `count = 1; ` ` `  `    ``// If word contain single letter, return 1. ` `    ``if` `(len == 1) ` `        ``return` `count; ` ` `  `    ``// Checking for first letter. ` `    ``if` `(str == str) ` `        ``count *= 1; ` `    ``else` `        ``count *= 2; ` ` `  `    ``// Traversing the string and multiplying ` `    ``// for combinations. ` `    ``for` `(``int` `j=1; j

## Java

 `// Java program to count words  whose ith letter ` `// is either (i-1)th, ith, or (i+1)th letter ` `// of given word. ` `public` `class` `GFG { ` ` `  `    ``// Return the count of words. ` `    ``static` `int` `countWords(String str, ``int` `len) ` `    ``{ ` `        ``int` `count = ``1``; ` `      `  `        ``// If word contain single letter, return 1. ` `        ``if` `(len == ``1``) ` `            ``return` `count; ` `      `  `        ``// Checking for first letter. ` `        ``if` `(str.charAt(``0``) == str.charAt(``1``)) ` `            ``count *= ``1``; ` `        ``else` `            ``count *= ``2``; ` `      `  `        ``// Traversing the string and multiplying ` `        ``// for combinations. ` `        ``for` `(``int` `j = ``1``; j < len - ``1``; j++) ` `        ``{ ` `            ``// If all three letters are same. ` `            ``if` `(str.charAt(j) == str.charAt(j - ``1``) &&  ` `                    ``str.charAt(j) == str.charAt(j + ``1``)) ` `                ``count *= ``1``; ` `      `  `            ``// If two letter are distinct. ` `            ``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``; ` `      `  `            ``// If all three letter are distinct. ` `            ``else` `                ``count *= ``3``; ` `        ``} ` `      `  `        ``// Checking for last letter. ` `        ``if` `(str.charAt(len - ``1``) == str.charAt(len - ``2``)) ` `            ``count *= ``1``; ` `        ``else` `            ``count *= ``2``; ` `      `  `        ``return` `count; ` `    ``} ` `      `  `    ``// Driven Program ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``String str = ``"abc"``; ` `        ``int` `len = str.length(); ` `      `  `        ``System.out.println(countWords(str, len)); ` `    ``} ` `} ` `// This code is contributed by Sumit Ghosh `

## Python 3

 `# Python 3 program to count words  whose ith letter ` `# is either (i-1)th, ith, or (i+1)th letter ` `# of given word. ` `  `  `# Return the count of words. ` `def` `countWords( ``str``,  l): ` ` `  `    ``count ``=` `1``; ` `  `  `    ``# If word contain single letter, return 1. ` `    ``if` `(l ``=``=` `1``): ` `        ``return` `count ` `  `  `    ``# Checking for first letter. ` `    ``if` `(``str``[``0``] ``=``=` `str``[``1``]): ` `        ``count ``*``=` `1` `    ``else``: ` `        ``count ``*``=` `2` `  `  `    ``# Traversing the string and multiplying ` `    ``# for combinations. ` `    ``for` `j ``in` `range``(``1``,l``-``1``): ` `        ``# If all three letters are same. ` `        ``if` `(``str``[j] ``=``=` `str``[j``-``1``] ``and` `str``[j] ``=``=` `str``[j``+``1``]): ` `            ``count ``*``=` `1` `  `  `        ``# If two letter are distinct. ` `        ``elif` `(``str``[j] ``=``=` `str``[j``-``1``] ``or` `                 ``str``[j] ``=``=` `str``[j``+``1``] ``or` `                 ``str``[j``-``1``] ``=``=` `str``[j``+``1``]): ` `            ``count ``*``=` `2` `  `  `        ``# If all three letter are distinct. ` `        ``else``: ` `            ``count ``*``=` `3` `  `  `    ``# Checking for last letter. ` `    ``if` `(``str``[l ``-` `1``] ``=``=` `str``[l ``-` `2``]): ` `        ``count ``*``=` `1` `    ``else``: ` `        ``count ``*``=` `2` `  `  `    ``return` `count ` `  `  `# Driven Program ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``str` `=` `"abc"` `    ``l ``=` `len``(``str``) ` `  `  `    ``print``(countWords(``str``, l)) `

## C#

 `// C# program to count words whose ` `// ith letter is either (i-1)th, ` `// ith, or (i+1)th letter of the ` `// given word ` `using` `System; ` ` `  `public` `class` `GFG { ` ` `  `    ``// Return the count of words. ` `    ``static` `int` `countWords(``string` `str, ``int` `len) ` `    ``{ ` `        ``int` `count = 1; ` ` `  `        ``// If word contain single letter, ` `        ``// return 1. ` `        ``if` `(len == 1) ` `            ``return` `count; ` ` `  `        ``// Checking for first letter. ` `        ``if` `(str == str) ` `            ``count *= 1; ` `        ``else` `            ``count *= 2; ` ` `  `        ``// Traversing the string and ` `        ``// multiplying for combinations. ` `        ``for` `(``int` `j = 1; j < len - 1; j++) { ` ` `  `            ``// If all three letters are same. ` `            ``if` `(str[j] == str[j - 1] && ` `                  ``str[j] == str[j + 1]) ` `                ``count *= 1; ` ` `  `            ``// If two letter are distinct. ` `            ``else` `if` `(str[j] == str[j - 1] || ` `                     ``str[j] == str[j + 1] || ` `                    ``str[j - 1] == str[j + 1]) ` `                ``count *= 2; ` ` `  `            ``// If all three letter are distinct. ` `            ``else` `                ``count *= 3; ` `        ``} ` ` `  `        ``// Checking for last letter. ` `        ``if` `(str[len - 1] == str[len - 2]) ` `            ``count *= 1; ` `        ``else` `            ``count *= 2; ` ` `  `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver Program ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``string` `str = ``"abc"``; ` `        ``int` `len = str.Length; ` ` `  `        ``Console.WriteLine(countWords(str, len)); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal `

## PHP

 ` `

Output:

```12
```

Time complexity : O(length of string).

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Improved By : nitin mittal, chitranayal

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.