Number of pairs of String whose concatenation leads to a Sorted string
Last Updated :
23 Jul, 2025
Given an array of strings S with the size of N where each string contains at least two distinct characters. The task is to find the number of pairs of strings ( s[i], s[j] ) whose concatenation forms a string that is alphabetically sorted. Here concatenation is simply the process of appending one string after another.
Examples:
Input: S[] = {"aax", "bbc", "xz"}
Output: 2
Explanation: The pairs that form a sorted string when concatenated are: {"aax", "xz"}, {"bbc", "xz"}
Input: S[] = {"fg", "mt", "cd"}
Output: 3
Explanation: The pairs that form a sorted string when concatenated are: {"cd", "fg"}, {"cd", "mt"}, {"fg", "mt"}
Naive Approach: The basic way to solve the problem is:
The brute force approach for this problem is to concatenate every pair of strings and then find out if the string formed is sorted or not.
Time complexity: O(N*N*max_size) where N is the size of the array of strings and max_size is the maximum size of the concatenation of two strings.
Auxiliary Space: O(1)
Efficient Approach: A better solution is based on these facts:
- If a string s1 is already unsorted and s2 is any random string then s1 + s2 will not form a sorted string.
- If the last character of a string s1 is lesser than or equal to the first character of another string s2 only then the string s1 + s2 can be sorted (provided both s1 and s2 are sorted).
Follow the below-mentioned steps to implement the above approach:
- Initialize a boolean array mark[] and label only those indexes as 1 for which s[i] is sorted and all other indexes as 0 (as to not consider them later).
- For each alphabet (a-z), calculate the number of sorted strings( mark[i] =1 ) that start with that particular alphabet and store the count in another array ( say nums ).
- For each string s[i], store the last character of that string in some last_char variable. A string that starts with an alphabet that comes after or equal to last_char can get appended to s[i] and it will always form a sorted string.
- Now iterate from the last_char till the last alphabet i.e. z and increment ans variable by the number of strings starting from each character involved in iteration.
- Return the answer.
Below is the implementation of the above approach:
C++
// C++ implementation of program
#include <bits/stdc++.h>
using namespace std;
// Check if a particular string is
// sorted or not
bool sorted(string s)
{
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] > s[i + 1])
return false;
}
return 1;
}
// Function to find the required
// number of pairs
int solve(string S[], int N)
{
// Boolean array mark to consider only
// those strings which are sorted and
// reject those which are not sorted
bool mark[N + 1] = { 0 };
for (int i = 0; i < N; i++) {
if (sorted(S[i])) {
mark[i] = 1;
}
}
// For every lower_case alphabet find out
// how many strings start with that
// particular alphabet
int nums[26] = { 0 };
for (int i = 0; i < N; i++) {
if (mark[i] == 1) {
int p = S[i][0] - 'a';
nums[p] += 1;
}
}
// Compute the answer for all
// the sorted strings
int ans = 0;
for (int i = 0; i < N; i++) {
if (mark[i] == 1) {
int len = S[i].size();
int last_char = S[i][len - 1] - 'a';
for (int j = last_char; j < 26; j++) {
ans += nums[j];
}
}
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
// Test case 1
string S[] = { "ac", "df", "pzz" };
int N = sizeof(S) / sizeof(S[0]);
// Function call
cout << solve(S, N) << endl;
// Test case 2
string S2[] = { "pqrs", "amq", "bcd" };
N = sizeof(S2) / sizeof(S2[0]);
// Function call
cout << solve(S2, N) << endl;
return 0;
}
Java
// Java code for the above approach
import java.util.Arrays;
public class Main {
// Check if a particular string is
// sorted or not
static boolean sorted(String s)
{
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) > s.charAt(i + 1)) {
return false;
}
}
return true;
}
// Function to find the required
// number of pairs
static int solve(String[] S, int N)
{
// Boolean array mark to consider only
// those strings which are sorted and
// reject those which are not sorted
boolean[] mark = new boolean[N + 1];
Arrays.fill(mark, false);
for (int i = 0; i < N; i++) {
if (sorted(S[i])) {
mark[i] = true;
}
}
// For every lower_case alphabet find out
// how many strings start with that
// particular alphabet
int[] nums = new int[26];
Arrays.fill(nums, 0);
for (int i = 0; i < N; i++) {
if (mark[i]) {
int p = S[i].charAt(0) - 'a';
nums[p] += 1;
}
}
// Compute the answer for all
// the sorted strings
int ans = 0;
for (int i = 0; i < N; i++) {
if (mark[i]) {
int len = S[i].length();
int lastChar = S[i].charAt(len - 1) - 'a';
for (int j = lastChar; j < 26; j++) {
ans += nums[j];
}
}
}
// Return the answer
return ans;
}
// Driver code
public static void main(String[] args)
{
// Test case 1
String[] S = { "ac", "df", "pzz" };
int N = S.length;
// Function call
System.out.println(solve(S, N));
// Test case 2
String[] S2 = { "pqrs", "amq", "bcd" };
N = S2.length;
// Function call
System.out.println(solve(S2, N));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python code for the above approach
# Check if a particular string is
# sorted or not
def sorted(s):
for i in range(len(s) - 1):
if s[i] > s[i + 1]:
return False
return True
# Function to find the required
# number of pairs
def solve(S, N):
# Boolean array mark to consider only
# those strings which are sorted and
# reject those which are not sorted
mark = [False] * (N + 1)
for i in range(N):
if sorted(S[i]):
mark[i] = True
# For every lower_case alphabet find out
# how many strings start with that
# particular alphabet
nums = [0] * 26
for i in range(N):
if mark[i]:
p = ord(S[i][0]) - ord('a')
nums[p] += 1
# Compute the answer for all
# the sorted strings
ans = 0
for i in range(N):
if mark[i]:
Len = len(S[i])
lastChar = ord(S[i][Len - 1]) - ord('a')
for j in range(lastChar, 26):
ans += nums[j]
# return the answer
return ans
# Test case 1
S = ["ac", "df", "pzz"]
N = len(S)
# Function call
print(solve(S, N))
# Test case 2
S2 = ["pqrs", "amq", "bcd"]
N = len(S2)
# Function call
print(solve(S2, N))
# This code is contributed by lokeshmvs21.
C#
// C# code for the above approach
using System;
public class GFG {
// Check if a particular string is
// sorted or not
static bool sorted(string s)
{
for (int i = 0; i < s.Length - 1; i++) {
if (s[i] > s[i + 1]) {
return false;
}
}
return true;
}
// Function to find the required
// number of pairs
static int solve(string[] S, int N)
{
// Boolean array mark to consider only
// those strings which are sorted and
// reject those which are not sorted
bool[] mark = new bool[N + 1];
for (int i = 0; i < N + 1; i++) {
mark[i] = false;
}
for (int i = 0; i < N; i++) {
if (sorted(S[i])) {
mark[i] = true;
}
}
// For every lower_case alphabet find out
// how many strings start with that
// particular alphabet
int[] nums = new int[26];
for (int i = 0; i < 26; i++) {
nums[i] = 0;
}
for (int i = 0; i < N; i++) {
if (mark[i]) {
int p = (int)S[i][0] - (int)'a';
nums[p] += 1;
}
}
// Compute the answer for all
// the sorted strings
int ans = 0;
for (int i = 0; i < N; i++) {
if (mark[i]) {
int len = S[i].Length;
int lastChar
= (int)S[i][len - 1] - (int)'a';
for (int j = lastChar; j < 26; j++) {
ans += nums[j];
}
}
}
// Return the answer
return ans;
}
static public void Main()
{
// Code
// Test case 1
string[] S = { "ac", "df", "pzz" };
int N = S.Length;
// Function call
Console.WriteLine(solve(S, N));
// Test case 2
string[] S2 = { "pqrs", "amq", "bcd" };
N = S2.Length;
// Function call
Console.WriteLine(solve(S2, N));
}
}
// This code is contributed by lokesh.
JavaScript
// Check if a particular string is
// sorted or not
function sorted(s) {
for (let i = 0; i < s.length - 1; i++) {
if (s[i] > s[i + 1]) return false;
}
return true;
}
// Function to find the required
// number of pairs
function solve(S, N) {
// Boolean array mark to consider only
// those strings which are sorted and
// reject those which are not sorted
const mark = new Array(N + 1).fill(false);
for (let i = 0; i < N; i++) {
if (sorted(S[i])) {
mark[i] = true;
}
}
// For every lower_case alphabet find out
// how many strings start with that
// particular alphabet
const nums = new Array(26).fill(0);
for (let i = 0; i < N; i++) {
if (mark[i]) {
const p = S[i][0].charCodeAt(0) - 'a'.charCodeAt(0);
nums[p] += 1;
}
}
// Compute the answer for all the sorted strings.
let ans = 0;
for (let i = 0; i < N; i++) {
if (mark[i]) {
const len = S[i].length;
const lastChar = S[i][len - 1].charCodeAt(0) - 'a'.charCodeAt(0);
for (let j = lastChar; j < 26; j++) {
ans += nums[j];
}
}
}
// Return the answer.
return ans;
}
// Test case 1
let S = [ "ac", "df", "pzz" ];
let N = S.length;
// Function call
console.log(solve(S, N));
// Test case 2
let S2 = [ "pqrs", "amq", "bcd" ];
N = S2.length;
// Function call
console.log(solve(S2, N));
// This code is contributed by akashish__
Time Complexity: O(N*MAX_SIZE), MAX_SIZE is the length of longest string
Auxiliary Space: O(N)
Related Articles:
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem