classSolution: defisPrefixOfWord(self, sentence: str, searchWord: str) -> int: i, index, n = 0, 1, len(sentence) while i < n: start = i while i < n and sentence[i] != ' ': i += 1 end = i if sentence[start:end].startswith(searchWord): return index index += 1 i += 1 return -1
classSolution { publicintisPrefixOfWord(String sentence, String searchWord) { intn= sentence.length(), index = 1, start = 0, end = 0; while (start < n) { while (end < n && sentence.charAt(end) != ' ') { end++; } if (isPrefix(sentence, start, end, searchWord)) { return index; }
index++; end++; start = end; } return -1; }
publicbooleanisPrefix(String sentence, int start, int end, String searchWord) { for (inti=0; i < searchWord.length(); i++) { if (start + i >= end || sentence.charAt(start + i) != searchWord.charAt(i)) { returnfalse; } } returntrue; } }
publicclassSolution { publicintIsPrefixOfWord(string sentence, string searchWord) { int n = sentence.Length, index = 1, start = 0, end = 0; while (start < n) { while (end < n && sentence[end] != ' ') { end++; } if (IsPrefix(sentence, start, end, searchWord)) { return index; }
index++; end++; start = end; } return-1; }
publicboolIsPrefix(string sentence, int start, int end, string searchWord) { for (int i = 0; i < searchWord.Length; i++) { if (start + i >= end || sentence[start + i] != searchWord[i]) { returnfalse; } } returntrue; } }
boolisPrefix(constchar* sentence, int start, int end, constchar* searchWord) { int len = strlen(searchWord); for (int i = 0; i < len; i++) { if (start + i >= end || sentence[start + i] != searchWord[i]) { returnfalse; } } returntrue; }
intisPrefixOfWord(char * sentence, char * searchWord){ int n = strlen(sentence), index = 1, start = 0, end = 0; while (start < n) { while (end < n && sentence[end] != ' ') { end++; } if (isPrefix(sentence, start, end, searchWord)) { return index; } index++; end++; start = end; } return-1; }
var isPrefixOfWord = function(sentence, searchWord) { let n = sentence.length, index = 1, start = 0, end = 0; while (start < n) { while (end < n && sentence[end] !== ' ') { end++; } if (isPrefix(sentence, start, end, searchWord)) { return index; }
index++; end++; start = end; } return -1; }
constisPrefix = (sentence, start, end, searchWord) => { for (let i = 0; i < searchWord.length; i++) { if (start + i >= end || sentence[start + i] !== searchWord[i]) { returnfalse; } } returntrue; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funcisPrefixOfWord(sentence, searchWord string)int { for i, index, n := 0, 1, len(sentence); i < n; i++ { start := i for i < n && sentence[i] != ' ' { i++ } end := i if strings.HasPrefix(sentence[start:end], searchWord) { return index } index++ } return-1 }