Count of character pairs at same distance as in English alphabets

Given a string, the task is to count the number of pairs whose elements are at same distances as in the English alphabets.
Note : Absolute distance between characters is considered.

Examples :

Input:  str = "geeksforgeeks"
Output:  4
Explanation: In this (g, s), (e, g), (e, k), (e, g) 
are the pairs that are at same distances as
in English alphabets.

Input:  str = "observation"
Output:  4
Explanation: (b, i), (s, v), (o, n), (v, t) are 
at same distances as in English alphabets.

A simple solution is to consider generate all pairs and compare pair characters with distance between them. If distance is same for a pair, then increment result.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Simple C++ program to find pairs with distance
// equal to English alphabet distance
#include <bits/stdc++.h>
using namespace std;
  
// Function to count pairs
int countPairs(string str)
{
    int result = 0;
    int n = str.length();
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
  
            // Increment count if characters are at
            // same distance
            if (abs(str[i] - str[j]) == abs(i - j))
                result++;
  
    return result;
}
  
// Driver code
int main()
{
    string str = "geeksforgeeks";
    cout << countPairs(str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Simple Java program to find pairs with distance
// equal to English alphabet distance
class Test {
      
    // Method to count pairs
    static int countPairs(String str)
    {
        int result = 0;
        int n = str.length();
        for (int i = 0; i < n; i++)
          for (int j = i + 1; j < n; j++)
              
            // Increment count if characters 
            // are at same distance
            if (Math.abs(str.charAt(i) - str.charAt(j)) ==
                                          Math.abs(i - j))
                result++;
  
        return result;
    }
  
    // Driver method
    public static void main(String args[])
    {
        String str = "geeksforgeeks";
        System.out.println(countPairs(str));
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Simple Python3 program to find pairs with 
# distance equal to English alphabet distance 
  
# Function to count pairs 
def countPairs(str1):
    result = 0
    n = len(str1) 
    for i in range(0, n):
        for j in range(i + 1, n): 
  
            # Increment count if characters 
            # are at same distance 
            if (abs(ord(str1[i]) - 
                    ord(str1[j])) == abs(i - j)): 
                result += 1
  
    return result; 
  
# Driver code 
if __name__ == "__main__"
    str1 = "geeksforgeeks"
    print(countPairs(str1)); 
  
# This code is contributed 
# by Sairahul099

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Simple C# program to find pairs with distance
// equal to English alphabet distance
using System;
  
class Test {
      
    // Method to count pairs
    static int countPairs(string str)
    {
        int result = 0;
        int n = str.Length;
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
              
            // Increment count if characters
            // are at same distance
            if (Math.Abs(str[i] - str[j]) == Math.Abs(i - j))
                result++;
  
        return result;
    }
  
    // Driver method
    public static void Main()
    {
        string str = "geeksforgeeks";
        Console.WriteLine(countPairs(str));
    }
}
  
// This Code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program for Count 
// of character pairs at 
// same distance as in 
// English alphabets
  
// Function to count pairs
function countPairs($str)
{
    $result = 0;
    $n = strlen($str);
    for ($i = 0; $i < $n; $i++)
        for ($j = $i + 1; 
             $j < $n; $j++)
  
            // Increment count if 
            // characters are at
            // same distance
            if (abs(ord($str[$i]) -
                    ord($str[$j])) == 
                    abs($i - $j))
                $result++;
  
    return $result;
}
  
// Driver code
$str = "geeksforgeeks";
echo countPairs($str);
  
// This code is contributed by Sam007
?>

chevron_right



Output:

4

Time complexity of above method is O(n2). The above method can be optimized by using the fact that there can be only 26 alphabets i.e. instead of checking an element upto length of string, check only from current index to 26th index.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// An otpimized C++ program to find pairs with distance
// equal to English alphabet distance
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
  
// Function to count pairs with distance
// equal to English alphabet distance
int countPairs(string str)
{
    int result = 0;
    int n = str.length();
  
    for (int i = 0; i < n; i++)
  
        // This loop runs at most 26 times
        for (int j = 1; (i + j) < n && j <= MAX_CHAR; j++)
            if ((abs(str[i + j] - str[i]) == j))
                result++;
  
    return result;
}
  
// Driver code
int main()
{
    string str = "geeksforgeeks";
    cout << countPairs(str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// An otpimized Java program to find pairs with distance
// equal to English alphabet distance
  
class Test {
    static final int MAX_CHAR = 26;
  
    // Method to count pairs with distance
    // equal to English alphabet distance
    static int countPairs(String str)
    {
        int result = 0;
        int n = str.length();
  
        for (int i = 0; i < n; i++)
  
            // This loop runs at most 26 times
            for (int j = 1; (i + j) < n && j <= MAX_CHAR; j++)
                if ((Math.abs(str.charAt(i + j) - str.charAt(i)) == j))
                    result++;
  
        return result;
    }
  
    // Driver method
    public static void main(String args[])
    {
        String str = "geeksforgeeks";
        System.out.println(countPairs(str));
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# An otpimized C++ program to find pairs with
# distance equal to English alphabet distance 
  
MAX_CHAR = 26
  
# Function to count pairs with distance 
# equal to English alphabet distance 
def countPairs(str1):
    result = 0
    n = len(str1) 
  
    for i in range(0, n): 
  
        # This loop runs at most 26 times
        for j in range(1, MAX_CHAR + 1):
            if((i + j) < n): 
                if ((abs(ord(str1[i + j]) -
                         ord(str1[i])) == j)):
                    result += 1
  
    return result 
  
# Driver code 
if __name__ == "__main__"
    str1 = "geeksforgeeks"
    print(countPairs(str1))
  
# This code is contributed
# by Sairahul099

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// An otpimized C# program to find pairs with distance
// equal to English alphabet distance
using System;
  
class Test {
      
    static int MAX_CHAR = 26;
  
    // Method to count pairs with distance
    // equal to English alphabet distance
    static int countPairs(string str)
    {
        int result = 0;
        int n = str.Length;
  
        for (int i = 0; i < n; i++)
  
            // This loop runs at most 26 times
            for (int j = 1; (i + j) < n && j <= MAX_CHAR; j++)
                if ((Math.Abs(str[i + j] - str[i]) == j))
                    result++;
  
        return result;
    }
  
    // Driver method
    public static void Main()
    {
        string str = "geeksforgeeks";
        Console.WriteLine(countPairs(str));
    }
}
  
// This Code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// An optimized PHP program 
// to find pairs with distance
// equal to English alphabet distance
  
// Function to count pairs 
// with distance equal to 
// English alphabet distance
function countPairs($str)
{
    $result = 0;
    $n = strlen($str);
  
    for ($i = 0; $i < $n; $i++)
  
        // This loop runs at
        // most 26 times
        for ($j = 1; ($i + $j) < $n &&
                      $j <= 26; $j++)
            if ((abs(ord($str[$i + $j]) - 
                     ord($str[$i]) ) == $j))
                $result++;
  
    return $result;
}
  
// Driver code
$str = "geeksforgeeks";
echo countPairs($str);
  
// This code is contributed by Sam007
?>

chevron_right



Output:

4

Time complexity of the optimized solution is O(n) under the assumption that alphabet size is constant.

This article is contributed by Sahil Chhabra (akku). 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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vt_m, Sam007, Sairahul Jella



Article Tags :
Practice Tags :


Be the First to upvote.


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