- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word in C++

We are given a string str[] as input. The goal is to count the words from str[] that have the same length as str[] and have positions of letters such that ith letter is replaced with letter at position (i1) or (i) or (i+1).

For the first letter replacement will be from position i or i+1

For the last letter replacement will be from position i-1 or i.

Let us understand with examples.

**Input** − str[] = “TPP”

**Output** − Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word are − 4

**Explanation**

Replacing T by T (i)^{th}or 1st P (i+1)^{th}= TPP, PPP Replacing 1st P by T (i-1)^{th}, P (i)th, or P(i+1)^{th}= TTP, TPP, TPP Replacing 2nd P by P(i-1)^{th}or P(i)^{th}= TPP, TPP Unique combination of replacements: TPP, PPP, TTP, PTP

**Input** − str = “aaa”

**Output** − Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word are: 1

**Explanation**

Replacing a by a (i)^{th}or 2nd a (i+1)^{th}= aaa, aaa Replacing 2nd a by a (i-1)^{th}, a (i)^{th}, or a(i+1)^{th}= aaa, aaa, aaa Replacing 3rd a by a(i-1)^{th}or a(i)^{th}= aaa, aaa Unique combination of replacements: aaa

## The approach used in the below program is as follows

We know that for every letter we have three possibilities. If for current letter i, all (i-1)th, ith, (i+1)th are different then we have 3 options. If two are the same we have 2 options, if all are the same then only one option.

So we will traverse the string and check for uniqueness and multiply with 3, 2 or 1 according to letters. For the first and last letter, we will check for uniqueness and multiply by 2 or 1 in similar manner.

Take string str[] as a character array.

Function total(char str[], int length) takes the string and returns the count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word in str[].

Take the initial count as 1. The word in str[] itself.

If there is a single letter, length will be 1, return 1.

Check for the first letter at index 0. If it is same as second then str[0]==str[1] then multiply count by 1

If they are different then multiply count by 2.

Now traverse from 2nd letter to second last character using for loop from index i=1 to i<length-1.

For each letter at index i. Check if str[i] is the same as str[i-1] or str[i+1]. If yes, multiply count by 1.

If any two are the same, multiply count by 2.

Else multiply count by 3.

For the last character, check if str[i-1]==str[i]. If true, multiply count by 1. Else multiply by 2

At the end we will have a count of distinct such words.

Return count as result.

## Example

#include<bits/stdc++.h> using namespace std; int total(char str[], int length){ int count = 1; if (length == 1){ return count; } if (str[0] == str[1]){ count = count * 1; } else{ count = count * 2; } for (int j=1; j<length-1; j++){ if (str[j] == str[j-1] && str[j] == str[j+1]){ count = count * 1; } else if (str[j] == str[j-1]){ count = count * 2; } else if(str[j] == str[j+1]){ count = count * 2; } else if(str[j-1] == str[j+1]){ count = count * 2; } else{ count = count * 3; } } if (str[length - 1] == str[length - 2]){ count = count * 1; } else{ count = count * 2; } return count; } int main(){ char str[] = "TPP"; int length = strlen(str); cout<<"Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word are: "<<total(str, length) << endl; return 0; }

## Output

If we run the above code it will generate the following output −

Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word are: 4

- Related Questions & Answers
- Check if sums of i-th row and i-th column are same in matrix in Python
- n-th number whose sum of digits is ten in C++
- k-th prime factor of a given number in java
- N-th root of a number in C++
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row in C++
- N-th Tribonacci Number in C++
- N-th polite number in C++
- Find M-th number whose repeated sum of digits of a number is N in C++
- Count ways to reach the n’th stair
- Check whether K-th bit is set or nots in Python
- HTML <th> colspan Attribute
- Find i’th index character in a binary string obtained after n iterations in C++
- K-th Element of Two Sorted Arrays in C++
- Find n-th node of inorder traversal in C++
- N’th palindrome of K digits in C++