Consider the following strings in an array defined in Java
G1
G12
G1-G2
G23
- If the user enters
G1, then the program should return the index ofG1andG1-G2- >[0, 2] - If the user enters
G2, the program should return the indexG1-G2->[2] - If the user enters
G12, the program should return the index ofG12->[1] - ...
One easy way to do that is to tokenize each array element with '-' and then use equals() for each element. That means an O(n^2) algorithm. Is there a better way to use regular expressions so that in one loop, the program searches for the pattern and use '-' as delimiting character?