-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily168.java
More file actions
56 lines (50 loc) · 1.4 KB
/
Copy pathdaily168.java
File metadata and controls
56 lines (50 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Solution 1
class Solution {
public int isPrefixOfWord(String sentence, String searchWord) {
/*
split string into word array
match characters until we find the right prefix
*/
String[] words = sentence.split("\\s+");
for (int i = 0; i < words.length; i++) {
String w = words[i];
int w_idx = 0;
int s_idx = 0;
while (s_idx < searchWord.length() && w_idx < w.length()) {
if (w.charAt(w_idx) != searchWord.charAt(s_idx))
break;
s_idx++;
w_idx++;
}
if (s_idx == searchWord.length())
return i + 1;
}
return -1;
}
}
// Solution 2
class Solution {
public int isPrefixOfWord(String sentence, String searchWord) {
String[] words = sentence.split(" ");
int len = searchWord.length();
String stri = "";
for(String str : words){
if(str.length()>=len && (str.substring(0,len).equals(searchWord))){
stri = str;
break;
}
}
int res = -1;
int count = 0;
if(stri.length()==0)
return -1;
for(String str: words){
count++;
if(stri.equals(str)){
res = count;
break;
}
}
return res;
}
}