12345678910111213141516171819202122232425262728293031 |
- /* A string contains the word "edabit" if a subsequence of its characters
- * spell "edabit".
- * Write a function that accepts a string and returns “YES” if the string
- * contains a subsequence of the word edabit or "NO" if it does not.
- */
- // size_t find (const string& str, size_t pos = 0) const;
- // found=str.find("needles are small",found+1,6);
- std::string edabitInString(std::string str) {
- // return std::string::npos != str.find("edabit",0) ? "YES" : "NO";
- std::string edabit = "edabit";
- int position = 0;
- // loop through the characters of the word edabit
- for(int i=0;i<edabit.size();i++){
- // keep track of if our current character in edabit has been found
- bool found = false;
- // continue from previous position, look for the current character
- for(;position<str.size();position++){
- // stop if we have a match
- if(str[position]==edabit[i]){
- found = true;
- break;
- }
- }
- // edabit cannot be spelled sequentially in the string
- if(!found){
- return "NO";
- }
- }
- // all characters of edabit have been found
- return "YES";
- }
|