[Practice]: (Contest Page | CodeChef)
Author: (codechefnitj | CodeChef User Profile for Codechef NITJ Chapter | CodeChef)
if -else condition, Loops
Chef is an introvert person. He has never talked someone over texts . Thus his uncle who is a software engineer has specially customized a software CAKEBOOK for him.
Chef wants to send hello to all his friends . Thus he types a string x . This software will convert string x to hello if some letters can be deleted from string x to form hello , otherwise sent message gets deleted.
You are given string x sent by chef to his friends . Tell whether chef has been able to say hello to his friends or NOT.
For example : if chef has sent message acolhepplmnloacolhepplmnlo to his friends the computer software will convert it to hello and it will be sent to his friends .
Our task is to find whether we can find the word hello in given string x.
Store hello in a string , let’s say string s . We will loop over both the strings at the same time. Let i be pointing to first character of string x (i=0), and j pointing to first character of string s “hello” (j=0) .
If character in string x matches character in string s we will increase both the pointers (i and j) otherwise we will increase only i.
Ending condition of loop will be ,if we reach end of any string we will stop.
After coming out of loop we will se if j pointer has reached the end of string s or not . If not , this means we were not able to find all characters of string s in string x.
Let’s define s to be hello
Now i=0 is pointing to character ‘a’ of string x and j=0 pointing to character ‘h’ of string s.
‘a’ doesn’t match with ‘h’ so only i is incremented. This continues till i becomes 4. Now x[i]=s[j] so both i and j are incremented. This continues till we reach end of any string and then we will check the condition that whether j=n or Not.
Time complexity is O(n) . We can ignore length of hello in comparison to n.
Every suggestion is welcome with open heart
Like if it made you understand the concept and problem