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
Definition
- Class:
- SearchBox
- Method:
- find
- Parameters:
- String, String, String, int
- Returns:
- int
- Method signature:
- int find(String text, String search, String wholeWord, int start)
- (be sure your method is public)
Constraints
- 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.
Examples
"We dont need no education"
"ed"
"N"
13
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"
"We"
"Y"
0
Returns: 0
"We" is exactly at the beginning of the text.
"No dark sarcasm in the classroom"
"The"
"Y"
5
Returns: -1
Remember that the search is case sensitive.
"Teachers leave them kids alone"
"kid"
"Y"
1
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"
"all"
"Y"
9
Returns: -1
"All in all youre just another brick in the wall"
"just"
"Y"
17
Returns: 17
"bAab aaBaBa Ab aa abbb ab ba aaba aab Ba aba aa a"
"Ba"
"N"
8
Returns: 9
"bAab aaBaBa Ab aa abbb ab ba aaba aab Ba aba aa a"
"Ba"
"N"
6
Returns: 7
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAB"
"N"
0
Returns: -1
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB"
"AAAAAAAAAAAAAAAAAAB"
"N"
0
Returns: 30
"abcd"
"abcd"
"Y"
0
Returns: 0
"abcd"
"abcd"
"Y"
1
Returns: -1
"abcd"
"abcd"
"Y"
3
Returns: -1
"B"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"N"
0
Returns: -1
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"Y"
0
Returns: 0
"A"
"C"
"N"
0
Returns: -1
"A"
"A"
"N"
0
Returns: 0
"A"
"A"
"Y"
0
Returns: 0
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB"
"N"
0
Returns: -1
"B"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"N"
0
Returns: -1
"A"
"A"
"Y"
0
Returns: 0
" "
"abc"
"Y"
5
Returns: -1
" abc"
"abc"
"Y"
5
Returns: 47
" abc"
"abc"
"Y"
0
Returns: 47
"abc"
"abcd"
"N"
0
Returns: -1
"abc d"
"abcd"
"N"
0
Returns: -1
"aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aa"
"aa"
"Y"
1
Returns: 48
"aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aa"
"aa"
"N"
1
Returns: 1
"Ho Ho Ho oh oh oh"
"ho"
"N"
0
Returns: -1
"xXwOU"
"xXwO"
"N"
2
Returns: -1
"UYwoWxwwxW"
"W"
"N"
4
Returns: 4
" x WuWoxWoYwOu OxWXYyoXuuXY xyOX xwouwUWUwXWWUX"
"xWuWoxWoYwOuOxWXY"
"Y"
0
Returns: -1
"yuOy WYYOwyOOoXoXxooOY"
"OO"
"Y"
8
Returns: -1
" Y xwywwWOyWYWoOyWwyuXwu uUwOuuxuXO xXYw"
"uxuXOxXY"
"N"
8
Returns: -1
"XuuUYYwyOUOwooyyOuYuy YYoXwXWuxOy"
"y"
"Y"
3
Returns: -1
"WWXY uoOXoy YXUXwwoWYwyWxuxWyUuxyXxy yyOUoWWWXX"
"XX"
"N"
33
Returns: 45
"wXw wYwOyuyWW"
"yWW"
"Y"
6
Returns: -1
"yXxWYoOWXWwwOOU O oXOwwx oWwxy OuyOUYYoYwWWwY "
"wwOOUOoXOwwxoWwx"
"N"
6
Returns: -1
"wWyWWwoW OwXWWOOWyWOU"
"wXW"
"N"
3
Returns: 10
"OuWwxyXYouoOUxuY OxXo OO X oOyW YXWuWyUWoUXUuXX"
"W"
"Y"
24
Returns: -1
"WOOxyOx"
"O"
"Y"
2
Returns: -1
"o OyoxxoYYXWuYO U oXxOxyu oOux"
"xO"
"N"
10
Returns: 20
"OYxxWYYywxxOOOOUyyuxYuUuXwyXwXxUoWyWyxxwy"
"OOUyyuxYuUuX"
"N"
1
Returns: 13
"wX xUwWxYwyOxYWWU YuxWwWxY"
"OxYWWUYuxWw"
"Y"
10
Returns: -1
"woWWxWxUWoYo"
"Y"
"N"
0
Returns: 10
"WowxYOuOUoOOXXoO WOXxxYwouuUYyXou"
"xYOuOUoOOXXoOWOXxxYwouu"
"Y"
14
Returns: -1
"XuXOOx Ox ywWXuu Uo"
"xOxyw"
"N"
3
Returns: -1
"wX wyWOwXo OyXoUxx xOYwXWWYWxx"
"WY"
"Y"
11
Returns: -1
" OYUWYOwwYxXyoXYXuwyOoO uOuy"
"OwwYxXyoXYXuwyOoOu"
"N"
4
Returns: -1
"OuuoWWyXxXoXwwWU o"
"wWU"
"Y"
3
Returns: -1
"oYY owxYyO O xXuyU xxY UWxYyUwoXooYuWoOXXwUY"
"WoOXX"
"Y"
29
Returns: -1
"yUuWoyOWyxo WXUoXXXWxWWUxOwXY yWXx"
"xWWUxOwXYyWX"
"Y"
4
Returns: -1
" xY YwxxXwXUxxyYww XX OwWxxYw w u"
"ww"
"Y"
25
Returns: -1
"yYyWuwYXUuxx XWUXoywy oUuyyOYUuxxuUw WXy"
"y"
"Y"
5
Returns: -1
"YuWYWxyXYyUXXwYo ooOuxUY"
"yXYyUXXwYo"
"N"
5
Returns: 6
"xXw UuWwyyuYUYY"
"xXwUuWwyyuYUYY"
"Y"
6
Returns: -1
"XuOyuXOYUxOuoWuWuwOwYXyXUyoXoOuw W"
"w"
"N"
8
Returns: 17
"yywWxYuYXxwox wOYXXuUwwxw xy "
"ywWxYuYXxwo"
"N"
28
Returns: -1
"OxwyWUyoyo oxxxwOuu wwOwXyOY"
"OwX"
"Y"
13
Returns: -1
"o"
"o"
"N"
0
Returns: 0
"XXu uUWoUOxOXOxy wwwyuYxYxYOoU"
"wwyuYxYxYOoU"
"Y"
17
Returns: -1
"xoWyUYxWWo"
"x"
"Y"
0
Returns: -1
"XXOxxYWYXUOYYxyoYOYoxOYXuuOyxxUUuXUYuxWWyXOXUxU "
"UUuXU"
"N"
43
Returns: -1
"UW Xw uUoouXuxO"
"o"
"Y"
6
Returns: -1
"uu"
"u"
"N"
1
Returns: 1
"OWUuWxYyywWuWUyxwXOWoU O"
"yywWuWUyxwXOWoU"
"N"
22
Returns: -1
"xyxYoxXxOWwWW "
"xXxOWwWW"
"Y"
4
Returns: -1
"oOYO oUyWxuUuWOwxwWy "
"YOoUyWxuUuWOwxwWy"
"N"
15
Returns: -1
"yWooXWX"
"X"
"N"
4
Returns: 4
"bAab aaBaBa Ab aa abbb ab abb aaba aab ab aba aa a"
"Ab"
"N"
49
Returns: -1
"abcd"
"cda"
"N"
1
Returns: -1
"bAab aaBaBa Ab aa abbb ab abb aaba aab ab aba aa a"
"Ab"
"Y"
49
Returns: -1
"abcd abc"
"abc"
"Y"
0
Returns: 5
"abc abc bc"
"bc"
"Y"
1
Returns: 8
"abc e"
"e"
"Y"
3
Returns: 4
"ab ba ab a"
"a"
"Y"
0
Returns: 9
"a abcd bcd"
"bcd"
"Y"
3
Returns: 7
"a"
"a"
"Y"
0
Returns: 0
"nott not"
"not"
"Y"
0
Returns: 5
"aaab aaa"
"aaa"
"Y"
0
Returns: 5
"abcd ef"
"bcd"
"Y"
1
Returns: -1
"aed ed"
"ed"
"Y"
0
Returns: 4
"abc e"
"e"
"Y"
4
Returns: 4
"Mymymy my"
"my"
"Y"
0
Returns: 7
"atest test"
"test"
"Y"
1
Returns: 6
"dwed we"
"we"
"Y"
0
Returns: 5
"h"
"h"
"Y"
0
Returns: 0
"Teachers leave them kid"
"kid"
"Y"
1
Returns: 20
"abc"
"abc"
"Y"
0
Returns: 0
"abc"
"d"
"N"
1
Returns: -1
"aaaa bbbbbbb aaaaa"
"bbbb"
"N"
0
Returns: 5
"this is a Test"
"Test"
"Y"
2
Returns: 10
"Teachers leave them kids alone kid haha"
"kid"
"Y"
1
Returns: 31
"raising the somehadkj adfhkajdshfjkadfasdklj the"
"the"
"Y"
15
Returns: 45
"yes i am fool so oo you"
"oo"
"Y"
0
Returns: 17
"aaBaa"
"B"
"Y"
0
Returns: -1
"xxxxabctt rabc uabc"
"abc"
"Y"
1
Returns: -1
"a thethe the"
"the"
"Y"
0
Returns: 9
"t"
"aaaaaaaaa"
"Y"
0
Returns: -1
"a"
"b"
"Y"
0
Returns: -1
"ab"
"b"
"Y"
1
Returns: -1
"a ba ab a"
"a"
"Y"
0
Returns: 0
" a We n"
"We"
"N"
0
Returns: 4
"my nameankur vijay"
"ankur"
"Y"
0
Returns: -1
"akademi mi"
"mi"
"Y"
0
Returns: 8
"Test"
"Test"
"Y"
0
Returns: 0
"abctt rabc uabc"
"abc"
"Y"
0
Returns: -1
"h"
"h"
"N"
0
Returns: 0
"Teachers leave them kids alone kids kid haha"
"kid"
"Y"
1
Returns: 36
" ab a"
"a"
"Y"
1
Returns: 4
"qwer"
"qwer"
"Y"
0
Returns: 0
"Wen We"
"We"
"Y"
0
Returns: 4
"gets started"
"get"
"Y"
0
Returns: -1
"Teachers lekidave them kid alone"
"kid"
"Y"
1
Returns: 23
"a"
"a"
"N"
0
Returns: 0
"thw def x"
"ef"
"Y"
5
Returns: -1
"kida kid"
"kid"
"Y"
0
Returns: 5
"astrb str "
"str"
"Y"
0
Returns: 6
"dabc x"
"abc"
"Y"
1
Returns: -1
"a a a a a"
"a"
"N"
1
Returns: 2
"ab c"
"b"
"Y"
1
Returns: -1
"Absd"
"ab"
"N"
0
Returns: -1
"We"
"e"
"Y"
1
Returns: -1
"bde ade cde"
"de"
"Y"
4
Returns: -1
"hoge"
"hoge"
"Y"
1
Returns: -1
"Alls All"
"All"
"Y"
0
Returns: 5
"aaa"
"aaa"
"Y"
0
Returns: 0
"aab aab aa"
"aa"
"Y"
0
Returns: 8
"en"
"en"
"Y"
0
Returns: 0
"challenge killer"
"cha"
"Y"
0
Returns: -1
"kid Teachers lekidave them kid alone"
"kid"
"Y"
1
Returns: 27
"All in all youre just another brick in the wall"
"wall"
"Y"
17
Returns: 43
"karim"
"s"
"N"
2
Returns: -1
"a a a"
"a"
"Y"
0
Returns: 0
"IamStupid x"
"Stupid"
"Y"
0
Returns: -1
"gfgdfabc "
"abc"
"Y"
0
Returns: -1
"We are"
"e"
"Y"
1
Returns: -1
"ab aabc abc"
"abc"
"Y"
1
Returns: 8
"Hello world hello world"
"orld"
"Y"
7
Returns: -1
"bkid"
"kid"
"Y"
1
Returns: -1
"text"
"text"
"Y"
0
Returns: 0
"H Y"
"Y"
"Y"
1
Returns: 2
"XABC ABC ABC"
"ABC"
"Y"
1
Returns: 5
"ab ba"
"b"
"Y"
1
Returns: -1
"baaaa"
"aa"
"Y"
0
Returns: -1
"All in all yourejust another brick in the wall"
"just"
"Y"
0
Returns: -1
"hi"
"hi"
"N"
0
Returns: 0
"abcabcd abcd"
"abcd"
"Y"
0
Returns: 8
"Danny danny"
"danny"
"N"
0
Returns: 6
"abra abra "
"abra"
"Y"
0
Returns: 0
"abc"
"abcbddjjdjdd"
"Y"
0
Returns: -1
"the"
"the"
"Y"
0
Returns: 0
"AB A"
"A"
"Y"
0
Returns: 3
"text fake"
"ake"
"Y"
6
Returns: -1
"abc"
"abcabcabc"
"N"
0
Returns: -1
"Ihavea have"
"have"
"Y"
1
Returns: 7
"Mymymy my"
"my"
"Y"
2
Returns: 7
"aba a"
"a"
"Y"
2
Returns: 4
"kid Teachers lekidave them kid alone"
"kid"
"Y"
1
Returns: 38
"test not tes"
"tes"
"Y"
0
Returns: 9
"abcd abcd bcd"
"bcd"
"Y"
6
Returns: 10