Advertisement
hr47

Untitled

May 23rd, 2020
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int isPrefixOfWord(string sentence, string searchWord) {
  4. vector<string> v;
  5. string a="";
  6. for(int i=0;i<sentence.size();i++){
  7. if(!isspace(sentence[i])){
  8. a+=sentence[i];
  9. }
  10. else{
  11. v.push_back(a);
  12. a="";
  13. }
  14. }
  15. v.push_back(a);
  16. int flag=-1;
  17. for(int i=0;i<v.size();i++){
  18. string a=v[i];
  19. if(a[0]==searchWord[0]){
  20. int k=i;
  21. for(int j=0;j<searchWord.size();j++){
  22. if(searchWord[j]!=a[j]){
  23. flag=-1;
  24. break;
  25. }
  26. else{
  27. flag=i;
  28. }
  29. }
  30. if(flag==k){
  31. return i+1;
  32. break;
  33. }
  34.  
  35.  
  36. }
  37.  
  38.  
  39.  
  40. }
  41. return -1;
  42. }
  43. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement