Binary Search 1 - String Find
You are given an array \(A\) of \(N\) unique alphabetically ordered strings and \(Q\) queries. Each query will contain a string \(Q_{i}\).
Output the indexes of each \(Q_{i}\) in the array \(A\). If \(Q_{i}\) does not exist in the array, output -1
.
Each string in \(A\) will only consist of lowercase letters and will be no longer than \(100\) characters long.
Constraints
\(1 \le N,Q \le 10 ^ 4\)
\(1 \le |A_{i}|, |Q_{i}| \le 10 ^ 2\)
Input Specification
The first line consists of \(N\) and \(Q\), representing the size of the array and the number of queries respectively.
The next \(N\) lines will each consist of a single string \(A_{i}\), representing the string at index \(i\) in the array \(A\). It is guaranteed that \(A\) is sorted lexicographically and contains no duplicates.
The following \(Q\) will each consist of a single string \(Q_{i}\), representing a string to look for in the array.
Output Specification
For each query \(Q_{i}\), output the index of \(Q_{i}\) in \(A\) on a new line. If \(Q_{i}\) does not exist in the array, output -1
instead.
Sample Input
5 3
abc
def
ghi
jkl
mno
ghi
abb
z
Sample Output
2
-1
-1
Explanation of Sample Output
The string "ghi"
is at index 2 in the array ["abc", "def", "ghi", "jkl", "mno"]
. Note that the array is zero-indexed.
The strings "abb"
and "z"
are nowhere to be found in the array. Thus, we output -1
for those queries.
Comments