Consider ARR = [“coding”, ”codezen”, ”codingninja”, ”coders”]
The longest common prefix among all the given strings is “cod” as it is present as a prefix in all strings. Hence, the answer is “cod”.
The first line of the input contains a single integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of strings in the array.
The next line contains ‘N’ space-separated strings denoting the elements of the array ‘ARR’.
For each test case, print a single string corresponding to the longest common prefix.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 3000
1 <= |ARR[i]| <=1000
Each string consists of only lowercase letters.
Time limit: 1 sec
In this approach, we will iterate through each character of the first string and check if the same character exists in each string or not. We will maintain a variable longestPrefix to store the longest common prefix.
We will traverse idx from 0 to the length of ARR[0] -1.
In the end, we will return the variable longestPrefix.
In this approach, we will divide the array of strings into halves, and we will find the common prefix for both parts separately. We will find the common prefix of these obtained strings.
We will define a function commonPrefix(STR1, STR2) to find the common prefix of STR1 and STR2. We will define a function commonPrefixUtil(ARR, start, end), returning the longest common prefix for the subarray from index start to end. This function will recursively divide the range into half and find the longest common prefix for that subarray of ARR.
Algorithm:
In this approach, we will first find the string with the minimum length among all the strings in the array and store it as prefix because the shortest string is the longest possible common prefix. We will also define a function isCommon(ARR, prefix, length, N) to whether the string prefix is a common prefix for all the strings in the array ARR or not. We will start the search by setting start as 0 and end as the length of prefix. At each iteration, we will set mid as (start+end)/2. Each time search space is divided into two equal parts, one of them is discarded because it is sure that it doesn't contain the solution. The element prefix[0: mid] is the substring of the prefix from index 0 to mid-1. There are two possible cases:
At last, we will return the substring of prefix from index 0 to mid.
Algorithm:
In this approach, we will use Trie to store all the strings, and then we will search for the longest common prefix using the Trie. Every node of Trie consists of multiple branches. Each branch represents a possible character of keys. We need to mark the last node of every key by setting endOfWord as true.
Hence our search is completed. We will also define a function insertWord(root, word) to insert a string word into the Trie.
Algorithm:
Batteries
Find The Single Element
Find The Single Element
Find minimum
Search In A Sorted 2D Matrix
Day 28 : Fake Coin Problem
Day 28 : Fake Coin Problem