Count number of equal pairs in a string
Given a string s, find the number of pairs of characters that are same. Pairs (s[i], s[j]), (s[j], s[i]), (s[i], s[i]), (s[j], s[j]) should be considered different.
Examples :
Input: air
Output: 3
Explanation :
3 pairs that are equal are (a, a), (i, i) and (r, r)
Input : w3wiki
Output : 31
The naive approach will be you to run two nested for loops and find out all pairs and keep a count of all pairs.
Steps to implement-
- Find the size of the given string
- Initialize a variable let us say “ans” with value 0 to store the answer
- Run two nested “for loop” from 0 to the size of string-1
- In that nested loop if two pair of characters are the same then increment that “ans” by 1
- In last return or print the value stored in “ans”
Code-
// CPP program to count the number of pairs
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of equal pairs
int countPairs(string s)
{
// Length of string
int n = s.size();
// Stores the answer
int ans = 0;
// Running nested loops to check equal pairs
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// When such pair got found
if (s[i] == s[j]) {
ans++;
}
}
}
return ans;
}
// Driver Code
int main()
{
string s = "w3wiki";
cout << countPairs(s);
return 0;
}
public class Main {
// Function to count the number of equal pairs
public static int countPairs(String s) {
// Length of string
int n = s.length();
// Stores the answer
int ans = 0;
// Running nested loops to check equal pairs
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// When such pair got found
if (s.charAt(i) == s.charAt(j)) {
ans++;
}
}
}
return ans;
}
// Driver Code
public static void main(String[] args) {
String s = "w3wiki";
System.out.println(countPairs(s));
}
}
// This code is contributed by rambabuguphka
# Function to count the number of equal pairs
def countPairs(s):
# Length of the string
n = len(s)
# Initialize the answer
ans = 0
# Running nested loops to check equal pairs
for i in range(n):
for j in range(n):
# When such pair is found
if s[i] == s[j]:
ans += 1
return ans
# Driver Code
if __name__ == "__main__":
s = "w3wiki"
print(countPairs(s))
using System;
public class GFG
{
// Function to count the number of equal pairs
public static int CountPairs(string s)
{
// Length of the string
int n = s.Length;
// Stores the answer
int ans = 0;
// Running nested loops to check equal pairs
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
// When such a pair is found
if (s[i] == s[j])
{
ans++;
}
}
}
return ans;
}
// Driver Code
public static void Main(string[] args)
{
string s = "w3wiki";
Console.WriteLine(CountPairs(s));
}
}
// JavaScript program to count the number of pairs
// Function to count the number of equal pairs
function countPairs(s)
{
// Length of string
let n = s.length;
// Stores the answer
let ans = 0;
// Running nested loops to check equal pairs
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
// When such pair got found
if (s[i] == s[j]) {
ans++;
}
}
}
return ans;
}
// Driver Code
let s = "w3wiki";
console.log(countPairs(s));
<?php
// Function to count the number of equal pairs
function countPairs($s) {
// Length of the string
$n = strlen($s);
// Initialize the answer
$ans = 0;
// Running nested loops to check equal pairs
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $n; $j++) {
// When such pair is found
if ($s[$i] == $s[$j]) {
$ans += 1;
}
}
}
return $ans;
}
// Driver Code
$s = "w3wiki";
echo countPairs($s);
?>
Output-
31
Time Complexity: O(N2), because of two nested loops
Auxiliary Space: O(1), because no extra space has been used
For an efficient approach, we need to count the number of equal pairs in linear time. Since pairs (x, y) and pairs (y, x) are considered different. We need to use a hash table to store the count of all occurrences of a character.So we know if a character occurs twice, then it will have 4 pairs – (i, i), (j, j), (i, j), (j, i). So using a hash function, store the occurrence of each character, then for each character the number of pairs will be occurrence^2. Hash table will be 256 in length as we have 256 characters.
Below is the implementation of the above approach :
// CPP program to count the number of pairs
#include <bits/stdc++.h>
using namespace std;
#define MAX 256
// Function to count the number of equal pairs
int countPairs(string s)
{
// Hash table
int cnt[MAX] = { 0 };
// Traverse the string and count occurrence
for (int i = 0; i < s.length(); i++)
cnt[s[i]]++;
// Stores the answer
int ans = 0;
// Traverse and check the occurrence of every character
for (int i = 0; i < MAX; i++)
ans += cnt[i] * cnt[i];
return ans;
}
// Driver Code
int main()
{
string s = "w3wiki";
cout << countPairs(s);
return 0;
}
// C program to count the number of equal pairs
#include <stdio.h>
#define MAX 256
// Function to count the number of equal pairs
int countPairs(char s[])
{
// Hash table
int cnt[MAX] = { 0 };
// Traverse the string and count occurrence
for (int i = 0; s[i] != '\0'; i++)
cnt[s[i]]++;
// Stores the answer
int ans = 0;
// Traverse and check the occurrence of every character
for (int i = 0; i < MAX; i++)
ans += cnt[i] * cnt[i];
return ans;
}
// Driver Code
int main()
{
char s[] = "w3wiki";
printf("%d",countPairs(s));
return 0;
}
// This code is contributed by sandeepkrsuman
// Java program to count the number of pairs
import java.io.*;
class GFG {
static int MAX = 256;
// Function to count the number of equal pairs
static int countPairs(String s)
{
// Hash table
int cnt[] = new int[MAX];
// Traverse the string and count occurrence
for (int i = 0; i < s.length(); i++)
cnt[s.charAt(i)]++;
// Stores the answer
int ans = 0;
// Traverse and check the occurrence
// of every character
for (int i = 0; i < MAX; i++)
ans += cnt[i] * cnt[i];
return ans;
}
// Driver Code
public static void main (String[] args)
{
String s = "w3wiki";
System.out.println(countPairs(s));
}
}
// This code is contributed by vt_m
# Python3 program to count the
# number of pairs
MAX = 256
# Function to count the number
# of equal pairs
def countPairs(s):
# Hash table
cnt = [0 for i in range(0, MAX)]
# Traverse the string and count
# occurrence
for i in range(len(s)):
cnt[ord(s[i]) - 97] += 1
# Stores the answer
ans = 0
# Traverse and check the occurrence
# of every character
for i in range(0, MAX):
ans += cnt[i] * cnt[i]
return ans
# Driver code
if __name__=="__main__":
s = "w3wiki"
print(countPairs(s))
# This code is contributed
# by Sairahul099
// C# program to count the number of pairs
using System;
class GFG {
static int MAX = 256;
// Function to count the number of equal pairs
static int countPairs(string s)
{
// Hash table
int []cnt = new int[MAX];
// Traverse the string and count occurrence
for (int i = 0; i < s.Length; i++)
cnt[s[i]]++;
// Stores the answer
int ans = 0;
// Traverse and check the occurrence
// of every character
for (int i = 0; i < MAX; i++)
ans += cnt[i] * cnt[i];
return ans;
}
// Driver Code
public static void Main ()
{
string s = "w3wiki";
Console.WriteLine(countPairs(s));
}
}
// This code is contributed by vt_m
// JavaScript program to count the
// number of pairs
const MAX = 256
// Function to count the number
// of equal pairs
function countPairs(s){
// Hash table
let cnt = new Array(MAX).fill(0)
// Traverse the string and count
// occurrence
for(let i=0;i<s.length;i++)
cnt[s.charCodeAt(i) - 97] += 1
// Stores the answer
let ans = 0
// Traverse and check the occurrence
// of every character
for(let i = 0; i < MAX; i++)
ans += cnt[i] * cnt[i]
return ans
}
// Driver code
let s = "w3wiki"
console.log(countPairs(s))
// This code is contributed
// by shinjanpatra
<?php
// Function to count the number of equal pairs
function countPairs($s) {
// Hash table
$cnt = array_fill(0, 256, 0);
// Traverse the string and count occurrence
for ($i = 0; $i < strlen($s); $i++) {
$cnt[ord($s[$i]) - 97]++;
}
// Stores the answer
$ans = 0;
// Traverse and check the occurrence of every character
for ($i = 0; $i < 256; $i++) {
$ans += $cnt[$i] * $cnt[$i];
}
return $ans;
}
// Driver code
$s = "w3wiki";
echo countPairs($s);
?>
Output
31
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n), where n is the length of the string
Contact Us