Given an array of strings arr[], count the number of pairs (i, j) such that i < j and the string arr[i] is both a prefix and a suffix of the string arr[j].
Examples:
Input: arr[] = ["gfg", "geeks", "gfg", "geeksforgeeks"]
Output: 2
Explanation: Valid pairs: [["gfg", "gfg"], ["geeks", "geeksforgeeks"]]Input: arr[] = ["ab", "abcab", "a", "aaab"]
Output: 1
Explanation: Valid pairs: ["ab", "abcab"]
[Approach]: Brute Force Prefix-Suffix Check
The idea is to iterate over all pairs of strings (i, j) such that i < j and check whether arr[i] matches both the starting and ending part of arr[j]. For each pair, we compare characters to verify prefix and suffix conditions, and if both match, we increment the count of valid pairs.
#include <iostream>
#include <vector>
using namespace std;
// Function to check if a is both prefix and suffix of b
bool isPrefixSuffix(string &a, string &b) {
int n = a.size();
int m = b.size();
if (n > m) return false;
string str1 = b.substr(0, n);
string str2 = b.substr(m - n, n);
return (str1 == a && str2 == a);
}
int countPairs(vector<string>& arr) {
int n = arr.size();
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (isPrefixSuffix(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
int main() {
vector<string> arr = {"gfg", "geeks", "gfg", "geeksforgeeks"};
cout << countPairs(arr);
return 0;
}
import java.util.*;
class GFG {
// Function to check if a is both prefix and suffix of b
static boolean isPrefixSuffix(String a, String b) {
int n = a.length();
int m = b.length();
if (n > m) return false;
String str1 = b.substring(0, n);
String str2 = b.substring(m - n);
return str1.equals(a) && str2.equals(a);
}
static int countPairs(String[] arr) {
int n = arr.length;
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (isPrefixSuffix(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
String[] arr = {"gfg", "geeks", "gfg", "geeksforgeeks"};
System.out.println(countPairs(arr));
}
}
# Function to check if a is both prefix and suffix of b
def isPrefixSuffix(a, b):
n = len(a)
m = len(b)
if n > m:
return False
str1 = b[:n]
str2 = b[m - n:]
return str1 == a and str2 == a
def countPairs(arr):
n = len(arr)
count = 0
for i in range(n):
for j in range(i + 1, n):
if isPrefixSuffix(arr[i], arr[j]):
count += 1
return count
if __name__ == "__main__":
arr = ["gfg", "geeks", "gfg", "geeksforgeeks"]
print(countPairs(arr))
using System;
class GFG {
// Function to check if a is both prefix and suffix of b
static bool isPrefixSuffix(string a, string b) {
int n = a.Length;
int m = b.Length;
if (n > m) return false;
string str1 = b.Substring(0, n);
string str2 = b.Substring(m - n);
return str1.Equals(a) && str2.Equals(a);
}
static int countPairs(string[] arr) {
int n = arr.Length;
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (isPrefixSuffix(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
static void Main() {
string[] arr = {"gfg", "geeks", "gfg", "geeksforgeeks"};
Console.WriteLine(countPairs(arr));
}
}
// Function to check if a is both prefix and suffix of b
function isPrefixSuffix(a, b) {
let n = a.length;
let m = b.length;
if (n > m) return false;
let str1 = b.slice(0, n);
let str2 = b.slice(m - n);
return str1 === a && str2 === a;
}
function countPairs(arr) {
let n = arr.length;
let count = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (isPrefixSuffix(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
// Driver Code
let arr = ["gfg", "geeks", "gfg", "geeksforgeeks"];
console.log(countPairs(arr));
Output
2
Time Complexity: O(n^2 * m) There are a total of (n(n − 1) / 2) pairs and for each pair we compare up to 'm' characters (length of the smaller string) to check both prefix and suffix.
Auxiliary Space: O(1)