Given a string, the task is to count the number of pairs whose elements are at the same distances as in the English alphabet.
Note : Absolute distance between characters is considered.
Examples :
Input: str = "neveropen"
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 generating all pairs and comparing pair characters with the distance between them. If distance is same for a pair, then increment result.
Algorithm:
- Step 1: Take an input string
- Step 2: Initialize result equals to 0 and n to the length of the string.
- Step 3: Using nested for loops check if the distance between characters is same
- Step 4: If distance is same increment the counter (result).
- Step 5: Print the output.
Below is the implementation of the above algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
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++)
if ( abs (str[i] - str[j]) == abs (i - j))
result++;
return result;
}
int main()
{
string str = "neveropen" ;
cout << countPairs(str);
return 0;
}
|
Java
import java.io.*;
class Test {
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++)
if (Math.abs(str.charAt(i) - str.charAt(j)) ==
Math.abs(i - j))
result++;
return result;
}
public static void main(String args[])
{
String str = "neveropen" ;
System.out.println(countPairs(str));
}
}
|
Python 3
def countPairs(str1):
result = 0 ;
n = len (str1)
for i in range ( 0 , n):
for j in range (i + 1 , n):
if ( abs ( ord (str1[i]) -
ord (str1[j])) = = abs (i - j)):
result + = 1 ;
return result;
if __name__ = = "__main__" :
str1 = "neveropen" ;
print (countPairs(str1));
|
C#
using System;
class Test {
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++)
if (Math.Abs(str[i] - str[j]) == Math.Abs(i - j))
result++;
return result;
}
public static void Main()
{
string str = "neveropen" ;
Console.WriteLine(countPairs(str));
}
}
|
PHP
<?php
function countPairs( $str )
{
$result = 0;
$n = strlen ( $str );
for ( $i = 0; $i < $n ; $i ++)
for ( $j = $i + 1;
$j < $n ; $j ++)
if ( abs (ord( $str [ $i ]) -
ord( $str [ $j ])) ==
abs ( $i - $j ))
$result ++;
return $result ;
}
$str = "neveropen" ;
echo countPairs( $str );
?>
|
Javascript
<script>
function countPairs(str)
{
let result = 0;
let n = str.length;
for (let i = 0; i < n; i++)
for (let j = i + 1; j < n; j++)
if (Math.abs(str[i].charCodeAt() - str[j].charCodeAt()) == Math.abs(i - j))
result++;
return result;
}
let str = "neveropen" ;
document.write(countPairs(str));
</script>
|
Time complexity: 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 up to length of string, check only from current index to 26th index.
Auxiliary Space: O(n), where n is the length of string. This is because when string is passed to any function it is passed by value and creates a copy of itself in stack.
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
int countPairs(string str)
{
int result = 0;
int n = str.length();
for ( int i = 0; i < n; i++)
for ( int j = 1; (i + j) < n && j <= MAX_CHAR; j++)
if (( abs (str[i + j] - str[i]) == j))
result++;
return result;
}
int main()
{
string str = "neveropen" ;
cout << countPairs(str);
return 0;
}
|
Java
import java.io.*;
class Test {
static final int MAX_CHAR = 26 ;
static int countPairs(String str)
{
int result = 0 ;
int n = str.length();
for ( int i = 0 ; i < n; i++)
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;
}
public static void main(String args[])
{
String str = "neveropen" ;
System.out.println(countPairs(str));
}
}
|
Python 3
MAX_CHAR = 26
def countPairs(str1):
result = 0 ;
n = len (str1)
for i in range ( 0 , n):
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
if __name__ = = "__main__" :
str1 = "neveropen" ;
print (countPairs(str1))
|
C#
using System;
class Test {
static int MAX_CHAR = 26;
static int countPairs( string str)
{
int result = 0;
int n = str.Length;
for ( int i = 0; i < n; i++)
for ( int j = 1; (i + j) < n && j <= MAX_CHAR; j++)
if ((Math.Abs(str[i + j] - str[i]) == j))
result++;
return result;
}
public static void Main()
{
string str = "neveropen" ;
Console.WriteLine(countPairs(str));
}
}
|
PHP
<?php
function countPairs( $str )
{
$result = 0;
$n = strlen ( $str );
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 1; ( $i + $j ) < $n &&
$j <= 26; $j ++)
if (( abs (ord( $str [ $i + $j ]) -
ord( $str [ $i ]) ) == $j ))
$result ++;
return $result ;
}
$str = "neveropen" ;
echo countPairs( $str );
?>
|
Javascript
<script>
let MAX_CHAR = 26;
function countPairs(str)
{
let result = 0;
let n = str.length;
for (let i = 0; i < n; i++)
for (let j = 1; (i + j) < n && j <= MAX_CHAR; j++)
if ((Math.abs(str[i + j].charCodeAt() - str[i].charCodeAt()) == j))
result++;
return result;
}
let str = "neveropen" ;
document.write(countPairs(str));
</script>
|
Time complexity: O(n2) under the assumption that alphabet size is constant.
Auxiliary Space: O(n), where n is the length of string. This is because when string is passed to any function it is passed by value and creates a copy of itself in stack.
This article is contributed by Sahil Chhabra (akku). If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!