code.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. /* A string contains the word "edabit" if a subsequence of its characters
  2. * spell "edabit".
  3. * Write a function that accepts a string and returns “YES” if the string
  4. * contains a subsequence of the word edabit or "NO" if it does not.
  5. */
  6. // size_t find (const string& str, size_t pos = 0) const;
  7. // found=str.find("needles are small",found+1,6);
  8. std::string edabitInString(std::string str) {
  9. // return std::string::npos != str.find("edabit",0) ? "YES" : "NO";
  10. std::string edabit = "edabit";
  11. int position = 0;
  12. // loop through the characters of the word edabit
  13. for(int i=0;i<edabit.size();i++){
  14. // keep track of if our current character in edabit has been found
  15. bool found = false;
  16. // continue from previous position, look for the current character
  17. for(;position<str.size();position++){
  18. // stop if we have a match
  19. if(str[position]==edabit[i]){
  20. found = true;
  21. break;
  22. }
  23. }
  24. // edabit cannot be spelled sequentially in the string
  25. if(!found){
  26. return "NO";
  27. }
  28. }
  29. // all characters of edabit have been found
  30. return "YES";
  31. }