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

 Live Demo

#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

Updated on: 02-Dec-2020

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements