Count pairs of characters in a string whose ASCII value difference is K

In this tutorial, we learn how to count the character pair in a string whose ASCII value is the difference of K. K is any difference value, it can be either 1 or 0. Create a C++ code to count such character pairs in an input string S. We used the size() method of the String class


size() = It is a String class method. It is an empty parameter library method. It returns the size of the string in terms of bytes. 
Example = s.size()

ASCII values are predefined values for characters, symbols, and others to be used for computer understanding.

The string used to count the pairs with ASCII difference of K (any defined value) contains only lower case alphabets.

Demonstration 1

Consider an example to understand the underlying key concept of the problem.

String = “abcb”
K = 0


 = 1

The character pair with a 0 ASCII value difference is only one in the given string and that pair is (b, b).

Demonstration 2

String = “abcd”
K = 1


= 3

The character pairs with ASCII value difference of 1 is 3 and such pairs are (a, b), (b, c), (c, d).


  • Create an array for the occurrence of the character.

  • When K = 0 (ASCII value difference is 0 only in similar repeating characters). Add the count by using the formula: character[loop variable] * (character [loop variable] -1)/2

  • When K = 1. Add characters occurrence and add this value to the counting variable.

  • Return the counting variable.

Logic 1 Example

We are implementing the above algorithm by using C++ programming. We defined a countDifferencePairs() function to count the number of required pairs. The function will find the character pairs whose ASCII value difference is 0.

We used the macro MAX to define its value as 26 and whenever it is called in the program the preprocessor will change the MAX to 26.

#include <bits/stdc++.h>
using namespace std;
#define MAX 26
//user-defined function to count the number of required pairs with K difference
int countDifferencePairs(string s, int x){
//This function returns the size of the string
   int l = s.size();
   //Storing the character frequency 
      int frq[MAX];
      memset(frq, 0, sizeof frq);
   for (int i = 0; i < l; i++)
      frq[s[i] - 'a']++;
   //counter variable to count the number of pairs
      int cp = 0;
   //If the condition to check the ASCII difference is 0 or not
      if (x == 0){
         for (int i = 0; i < MAX; i++)
            if (frq[i] > 1)
               cp += ((frq[i] * (frq[i] - 1)) / 2);
   else {
      for (int i = 0; i < MAX; i++)
         if (frq[i] > 0 && i + x < MAX && frq[i + x] > 0)
            cp += (frq[i] * frq[i + x]);;
   return cp;
// Controlling Part
int main(){
   string s = "abcda";
   int x = 0;
   cout <<"Pairs with given ascii values are:"<<  countDifferencePairs(s, x);
   return 0;


Pairs with given ascii values are: 1


In this tutorial, we develop an approach to counting the character pairs of an input string whose ASCII value difference is K. The K can be any value and in the C++ implementation example, we used a 0 difference value. We used the size() function of the string class. During the implementation, we considered only those character pairs of the input string whose ASCII value difference is 0.

Updated on: 31-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started