TopCoder problem "SearchBox" used in SRM 361 (Division II Level One)

Problem Statement


You are writing a simple text editor, and one of the features you need to implement is a text search. Given a pattern, the search mechanism should return the position of its first occurrence, starting from the current position, or indicate that the pattern cannot be found. Searches are case sensitive, and do not wrap. The search dialog contains a text box and a checkbox labeled "Whole Word". When the "Whole Word" option is selected, the matched sequence must either be preceded by a space to its left or be at the very beginning of the text. Similarly, it must also either be followed by a space to its right or be at the very end of the text.

You are given a String text consisting of letters and spaces. You are also given a String search, a sequence of letters representing the search pattern, and a String wholeWord, which is either "Y" or "N", indicating whether or not the "Whole Word" option is checked. An int start represents the current position in the text from where you start searching. Return the index of the first match, or -1 if there is none. The index of the match here means the index of its first character. Both the starting index and the return index are zero based.



Parameters:String, String, String, int
Method signature:int find(String text, String search, String wholeWord, int start)
(be sure your method is public)


-text will contain between 1 and 50 characters, inclusive.
-text will contain only letters ('a'-'z', 'A'-'Z') and spaces.
-search will contain between 1 and 50 characters, inclusive.
-search will contain only letters ('a'-'z', 'A'-'Z').
-wholeWord will contain a single letter 'Y' or 'N'.
-start will be between 0 and N-1, inclusive, where N is the number of characters in text.


"We dont need no education"
Returns: 16
The whole text has two occurrences of "ed", but since we start the search at the 13th character we find the second one.
"We dont need no thought control"
Returns: 0
"We" is exactly at the beginning of the text.
"No dark sarcasm in the classroom"
Returns: -1
Remember that the search is case sensitive.
"Teachers leave them kids alone"
Returns: -1
With the "Whole Word" option selected the "kid" won't be matched even though it's present in "kids".
"All in all its just another brick in the wall"
Returns: -1
"All in all youre just another brick in the wall"
Returns: 17

Problem url:

Problem stats url:




PabloGilberto , Yarin , Olexiy , ivan_metelsky

Problem categories:

Simple Search, Iteration, String Manipulation