Search a string in the dictionary with a given prefix and suffix for Q queries
Last Updated :
23 Jul, 2025
Given an array arr[] consisting of N strings and Q queries in form of two strings prefix and suffix, the task for each query is to find any one string in the given array with the given prefix and suffix. If there exists no such string then print "-1".
Examples:
Input: arr[] = {"apple", "app", "biscuit", "mouse", "orange", "bat", "microphone", "mine"}, Queries[] = {{a, e}, {a, p}}
Output:
apple
app
Explanation:
Query 1: String "apple" is the only word in the given dictionary with the given prefix "a" and suffix "e".
Query 2: String "app" is the only word in the given dictionary with the given prefix "a" and suffix "p".
Input: arr[] = {"apple", "app", "biscuit", "mouse", "orange", "bat", "microphone", "mine"}, Queries[] = {{mi, ne}}
Output: mine
Naive Approach: The simplest approach to solve the given problem is to traverse the given array of strings arr[] for each query and if there exists any such string with the given prefix and suffix, then print that string. Otherwise, print "-1".
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
// N is the number of strings in the array
int N=8;
// Declare an array of size N
string arr[]
= { "apple", "app", "biscuit",
"mouse", "orange", "bat",
"mine", "mine" };
vector<vector<string> > Q = { { "a", "e" }, { "mi", "ne" } };
// Taking input the queries
string prefix, suffix;
for(int i=0; i<Q.size(); i++)
{
prefix = Q[i][0];
suffix = Q[i][1];
// Flag to check if string is found
// with given prefix and suffix
int found = 0;
// Check every string in arr[]
for(int j=0; j<N; j++)
{
// If string is found with given
// prefix and suffix
if(arr[j].find(prefix) == 0 && arr[j].rfind(suffix) == arr[j].length()-suffix.length())
{
// Print the string
cout<<arr[j]<<endl;
// Set flag to 1 to indicate that
// string is found
found = 1;
break;
}
}
if(found == 0)
cout<<"-1"<<endl;
}
return 0;
}
Java
// Java code for above approach
import java.util.*;
public class Main {
public static void main(String[] args) {
// N is the number of strings in the array
int N = 8;
// Declare an array of size N
String[] arr = {"apple", "app", "biscuit", "mouse", "orange", "bat", "mine", "mine"};
List<List<String>> Q = new ArrayList<>();
Q.add(Arrays.asList("a", "e"));
Q.add(Arrays.asList("mi", "ne"));
// Taking input the queries
String prefix, suffix;
for (int i = 0; i < Q.size(); i++) {
prefix = Q.get(i).get(0);
suffix = Q.get(i).get(1);
// Flag to check if string is found
// with given prefix and suffix
int found = 0;
// Check every string in arr[]
for (int j = 0; j < N; j++) {
// If string is found with given
// prefix and suffix
if (arr[j].indexOf(prefix) == 0 && arr[j].lastIndexOf(suffix) == arr[j].length() - suffix.length()) {
// Print the string
System.out.println(arr[j]);
// Set flag to 1 to indicate that
// string is found
found = 1;
break;
}
}
if (found == 0)
System.out.println("-1");
}
}
}
// This code is contributed by Utkarsh Kumar.
Python3
# Python program for the above approach
# N is the number of strings in the array
N = 8
# Declare an array of size N
arr = ["apple", "app", "biscuit", "mouse", "orange", "bat", "mine", "mine"]
Q = [["a", "e"], ["mi", "ne"]]
for i in range(len(Q)):
# Taking input the queries
prefix = Q[i][0]
suffix = Q[i][1]
# Flag to check if string is found
# with given prefix and suffix
found = 0
# Check every string in arr[]
for j in range(N):
# If string is found with given
# prefix and suffix
if arr[j].find(prefix) == 0 and arr[j].rfind(suffix) == len(arr[j]) - len(suffix):
# Print the string
print(arr[j])
# Set flag to 1 to indicate that
# string is found
found = 1
break
if found == 0:
print("-1")
# This code is contributed by rishabmalhdjio
C#
// C# Program for the above approach
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// N is the number of strings in the array
int N = 8;
// Declare an array of size N
string[] arr = { "apple", "app", "biscuit", "mouse", "orange", "bat", "mine", "mine" };
List<List<string>> Q = new List<List<string>> { new List<string> { "a", "e" }, new List<string> { "mi", "ne" } };
// Taking input the queries
string prefix, suffix;
for (int i = 0; i < Q.Count; i++)
{
prefix = Q[i][0];
suffix = Q[i][1];
// Flag to check if string is found
// with given prefix and suffix
int found = 0;
// Check every string in arr[]
for (int j = 0; j < N; j++)
{
// If string is found with given
// prefix and suffix
if (arr[j].IndexOf(prefix) == 0 && arr[j].LastIndexOf(suffix) == arr[j].Length - suffix.Length)
{
// Print the string
Console.WriteLine(arr[j]);
// Set flag to 1 to indicate that
// string is found
found = 1;
break;
}
}
if (found == 0)
Console.WriteLine("-1");
}
}
}
// Contributed by rishabmalhdijo
JavaScript
// N is the number of strings in the array
const N = 8;
// Declare an array of size N
const arr = ["apple", "app", "biscuit", "mouse", "orange", "bat", "mine", "mine"];
const Q = [["a", "e"], ["mi", "ne"]];
// Taking input the queries
let prefix, suffix;
for (let i = 0; i < Q.length; i++) {
prefix = Q[i][0];
suffix = Q[i][1];
// Flag to check if string is found
// with given prefix and suffix
let found = 0;
// Check every string in arr[]
for (let j = 0; j < N; j++) {
// If string is found with given
// prefix and suffix
if (arr[j].startsWith(prefix) && arr[j].endsWith(suffix)) {
// Print the string
console.log(arr[j]);
// Set flag to 1 to indicate that
// string is found
found = 1;
break;
}
}
if (found == 0) console.log("-1");
}
Time Complexity: O(Q*N*M), where M is the maximum length of the string.
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using the Trie Data Structure to solve the problem. The implementation of trie can be modified to support both prefix and suffix search in the following way:
- Suppose the given word in a dictionary is apple. In this case, the word apple is inserted in the Trie. But to support prefix and suffix search simultaneously the words: e{apple, le{apple, ple{apple, pple{apple, apple{apple can be inserted in the Trie.
- Note that these words are of the form suffix{word where suffix is the all suffixes possible from the given word.
- The special character { is inserted between the suffix and word to separate them. Any special character other than the alphabets can be used in place of {, but { is preferred because its ASCII value is 123 which is one more than the ASCII value of z.
Follow the steps below to solve the problem:
- Traverse all the strings in the array arr[] using the variable i and perform the following steps:
- After the creation of the trie, iterate over all the queries and perform the following steps:
- Store the prefix and suffix string for the current query.
- Initialize a string, t as suffix + '{' + prefix and search for it in the trie.
- If it is not found, then print "-1". Otherwise, print the matching string.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Trie Node
struct Trie {
Trie* arr[27] = { NULL };
// Stores the index of the word
// in the dictionary
int idx;
};
// Root node of the Trie
Trie* root = new Trie();
// Function to insert the words in
// the Trie
void insert(string word, int i)
{
// Temporary pointer to the root
// node of the Trie
Trie* temp = root;
// Traverse through all the
// characters of current word
for (char ch : word) {
// Make a Trie Node, if not
// already present
if (temp->arr[ch - 'a'] == NULL) {
Trie* t = new Trie();
temp->arr[ch - 'a'] = t;
}
temp = temp->arr[ch - 'a'];
temp->idx = i;
}
}
// Function to search the words in Trie
int search(string word)
{
Trie* temp = root;
// Traverse through all the
// characters of current word
for (char ch : word) {
// If no valid Trie Node exists
// for the current character
// then there is no match
if (temp->arr[ch - 'a'] == NULL)
return -1;
temp = temp->arr[ch - 'a'];
}
// Return the resultant index
return temp->idx;
}
// Function to search for a word in
// the dictionary with the given
// prefix and suffix for each query
void findMatchingString(
string words[], int n,
vector<vector<string> > Q)
{
string temp, t;
// Insertion in the Trie
for (int i = 0; i < n; i++) {
// Form all the words of the
// form suffix{word and insert
// them in the trie
temp = "{" + words[i];
for (int j = words[i].size() - 1;
j >= 0; j--) {
t = words[i][j] + temp;
temp = t;
// Insert into Trie
insert(t, i);
}
}
// Traverse all the queries
for (int i = 0; i < Q.size(); i++) {
string prefix = Q[i][0];
string suffix = Q[i][1];
string temp = suffix + "{" + prefix;
// Stores the index of
// the required word
int res;
// Store the index of the
// word in the dictionary
res = search(temp);
// In case of match, print
// the corresponding string
if (res != -1) {
cout << words[res] << '\n';
}
// Otherwise, No match found
else
cout << "-1\n";
}
}
// Driver Code
int main()
{
string arr[]
= { "apple", "app", "biscuit",
"mouse", "orange", "bat",
"microphone", "mine" };
int N = 8;
vector<vector<string> > Q = { { "a", "e" }, { "mi", "ne" } };
findMatchingString(arr, N, Q);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Trie Node
public static class Trie {
Trie []arr = new Trie[27];
// Stores the index of the word
// in the dictionary
int idx;
};
// Root node of the Trie
public static Trie root;
// Function to insert the words in
// the Trie
public static void insert(String word, int i)
{
// Temporary pointer to the root
// node of the Trie
Trie temp = root;
// Traverse through all the
// characters of current word
for (int ch=0; ch < word.length(); ch++){
// Make a Trie Node, if not
// already present
if (temp.arr[word.charAt(ch) - 'a'] == null) {
Trie t = new Trie();
temp.arr[word.charAt(ch) - 'a'] = t;
}
temp = temp.arr[word.charAt(ch) - 'a'];
temp.idx = i;
}
}
// Function to search the words in Trie
public static int search(String word)
{
Trie temp = root;
// Traverse through all the
// characters of current word
for (int ch=0; ch < word.length(); ch++) {
// If no valid Trie Node exists
// for the current character
// then there is no match
if (temp.arr[word.charAt(ch) - 'a'] == null)
return -1;
temp = temp.arr[word.charAt(ch) - 'a'];
}
// Return the resultant index
return temp.idx;
}
// Function to search for a word in
// the dictionary with the given
// prefix and suffix for each query
public static void findMatchingString(
String[] words, int n,
String[][] Q)
{
String temp, t="";
// Insertion in the Trie
for (int i = 0; i < n; i++) {
// Form all the words of the
// form suffix{word and insert
// them in the trie
temp = "{" + words[i];
for (int j = words[i].length() - 1;
j >= 0; j--) {
t = words[i].charAt(j) + temp;
temp = t;
// Insert into Trie
insert(t, i);
}
}
// Traverse all the queries
for (int i = 0; i < Q.length ; i++) {
String prefix = Q[i][0];
String suffix = Q[i][1];
temp = suffix + "{" + prefix;
// Stores the index of
// the required word
int res;
// Store the index of the
// word in the dictionary
res = search(temp);
// In case of match, print
// the corresponding string
if (res != -1) {
System.out.println(words[res]);
}
// Otherwise, No match found
else
System.out.println("-1");
}
}
public static void main (String[] args) {
String[] arr
= { "apple", "app", "biscuit",
"mouse", "orange", "bat",
"microphone", "mine" };
int N = 8;
root=new Trie();
String[][] Q = { { "a", "e" }, { "mi", "ne" } };
findMatchingString(arr, N, Q);
}
}
// This code is contributed by Aman Kumar.
Python3
# Trie Node
class TrieNode:
def __init__(self):
self.arr = [None] * 27
self.idx = None
# Root node of the Trie
root = TrieNode()
# Function to insert the words in
# the Trie
def insert(word, i):
# Temporary pointer to the root
# node of the Trie
temp = root
# Traverse through all the
# characters of current word
for ch in word:
# Make a Trie Node, if not
# already present
if temp.arr[ord(ch) - ord('a')] is None:
t = TrieNode()
temp.arr[ord(ch) - ord('a')] = t
temp = temp.arr[ord(ch) - ord('a')]
temp.idx = i
# Function to search the words in Trie
def search(word):
temp = root
# Traverse through all the
# characters of current word
for ch in word:
# If no valid Trie Node exists
# for the current character
# then there is no match
if temp.arr[ord(ch) - ord('a')] is None:
return -1
temp = temp.arr[ord(ch) - ord('a')]
# Return the resultant index
return temp.idx
# Function to search for a word in
# the dictionary with the given
# prefix and suffix for each query
def findMatchingString(words, Q):
# Insertion in the Trie
for i in range(len(words)):
# Form all the words of the
# form suffix{word and insert
# them in the trie
temp = "{" + words[i]
for j in range(len(words[i]) - 1, -1, -1):
t = words[i][j] + temp
temp = t
# Insert into Trie
insert(t, i)
# Traverse all the queries
for i in range(len(Q)):
prefix = Q[i][0]
suffix = Q[i][1]
temp = suffix + "{" + prefix
# Stores the index of
# the required word
res = search(temp)
# In case of match, print
# the corresponding string
if res != -1:
print(words[res])
# Otherwise, No match found
else:
print("-1")
# Driver Code
if __name__ == "__main__":
words = ["apple", "app", "biscuit", "mouse", "orange", "bat", "microphone", "mine"]
Q = [['a', 'e'], ['mi', 'ne']]
findMatchingString(words, Q)
C#
// C# program for the above approach
using System;
class GFG {
// Trie Node
public class Trie {
public Trie[] arr = new Trie[27];
// Stores the index of the word
// in the dictionary
public int idx;
};
// Root node of the Trie
public static Trie root;
// Function to insert the words in
// the Trie
public static void insert(String word, int i)
{
// Temporary pointer to the root
// node of the Trie
Trie temp = root;
// Traverse through all the
// characters of current word
for (int ch = 0; ch < word.Length; ch++) {
// Make a Trie Node, if not
// already present
if (temp.arr[word[ch] - 'a'] == null) {
Trie t = new Trie();
temp.arr[word[ch] - 'a'] = t;
}
temp = temp.arr[word[ch] - 'a'];
temp.idx = i;
}
}
// Function to search the words in Trie
public static int search(String word)
{
Trie temp = root;
// Traverse through all the
// characters of current word
for (int ch = 0; ch < word.Length; ch++) {
// If no valid Trie Node exists
// for the current character
// then there is no match
if (temp.arr[word[ch] - 'a'] == null)
return -1;
temp = temp.arr[word[ch] - 'a'];
}
// Return the resultant index
return temp.idx;
}
// Function to search for a word in
// the dictionary with the given
// prefix and suffix for each query
public static void
findMatchingString(String[] words, int n, String[][] Q)
{
String temp, t = "";
// Insertion in the Trie
for (int i = 0; i < n; i++) {
// Form all the words of the
// form suffix{word and insert
// them in the trie
temp = "{" + words[i];
for (int j = words[i].Length - 1; j >= 0; j--) {
t = words[i][j] + temp;
temp = t;
// Insert into Trie
insert(t, i);
}
}
// Traverse all the queries
for (int i = 0; i < Q.Length; i++) {
String prefix = Q[i][0];
String suffix = Q[i][1];
temp = suffix + "{" + prefix;
// Stores the index of
// the required word
int res;
// Store the index of the
// word in the dictionary
res = search(temp);
// In case of match, print
// the corresponding string
if (res != -1) {
Console.WriteLine(words[res]);
}
// Otherwise, No match found
else
Console.WriteLine("-1");
}
}
public static void Main(String[] args)
{
String[] arr
= { "apple", "app", "biscuit", "mouse",
"orange", "bat", "microphone", "mine" };
int N = 8;
root = new Trie();
String[][] Q = { new String[] { "a", "e" },
new String[] { "mi", "ne" } };
findMatchingString(arr, N, Q);
}
}
// This code is contributed by ishankhandelwals.
JavaScript
// Javascript program for the above approach
// Trie Node
class Trie {
constructor() {
this.arr = new Array(27).fill(null);
this.idx = null;
}
}
// Root node of the Trie
let root = new Trie();
// Function to insert the words in
// the Trie
function insert(word, i) {
// Temporary pointer to the root
// node of the Trie
let temp = root;
// Traverse through all the
// characters of current word
for (let ch of word) {
// Make a Trie Node, if not
// already present
if (temp.arr[ch.charCodeAt(0) - 97] == null) {
temp.arr[ch.charCodeAt(0) - 97] = new Trie();
}
temp = temp.arr[ch.charCodeAt(0) - 97];
temp.idx = i;
}
}
// Function to search the words in Trie
function search(word) {
let temp = root;
// Traverse through all the
// characters of current word
for (let ch of word) {
// If no valid Trie Node exists
// for the current character
// then there is no match
if (temp.arr[ch.charCodeAt(0) - 97] == null) return -1;
temp = temp.arr[ch.charCodeAt(0) - 97];
}
// Return the resultant index
return temp.idx;
}
// Function to search for a word in
// the dictionary with the given
// prefix and suffix for each query
function findMatchingString(words, n, Q) {
let temp, t;
// Insertion in the Trie
for (let i = 0; i < n; i++) {
// Form all the words of the
// form suffix{word and insert
// them in the trie
temp = "{" + words[i];
for (let j = words[i].length - 1; j >= 0; j--) {
t = words[i][j] + temp;
temp = t;
// Insert into Trie
insert(t, i);
}
}
// Traverse all the queries
for (let i = 0; i < Q.length; i++) {
let prefix = Q[i][0];
let suffix = Q[i][1];
let temp = suffix + "{" + prefix;
// Stores the index of
// the required word
let res;
// Store the index of the
// word in the dictionary
res = search(temp);
// In case of match, print
// the corresponding string
if (res != -1) {
console.log(words[res]);
}
// Otherwise, No match found
else console.log(-1);
}
}
// Driver Code
let arr = ["apple", "app", "biscuit", "mouse", "orange", "bat", "microphone", "mine"];
let N = 8;
let Q = [["a", "e"], ["mi", "ne"]];
findMatchingString(arr, N, Q);
// This code is contributed by ishankhandelwals.
Time Complexity: O(N*M2 + Q*M), where M is the maximum length of among all the strings
Auxiliary Space: O(N*M2)
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