Work on algorithm called "Remove Vowels From A String in Place ".
problem statement see the blog:
https://www.careercup.com/question?id=2698
Take any word, remove vowels and place them in reverse order by untouching consonants and place them back. Word is AIRPLANE.
HackerRank has one algorithm, but only 4 submissions:
https://www.hackerrank.com/contests/knockout-coding/challenges/pair-odd
Read the blog about "Remove vowels":
http://shashank7s.blogspot.ca/2011/03/wap-to-remove-remove-vowels-from-string.html
https://www.quora.com/How-do-I-remove-vowels-from-a-string
Question and Answer:
Walk through the code, add comments:
const string vowels = "aeiou"; // Julia: all vowels in a string
void remVowels(string &s){
int k=0; // pointer: int k, mark the next available position for non vowel char
for(int i=0; i<s.length(); i++){ // Julia: go through the string once, in a loop
if(vowels.find(s[i]) == string::npos) // if current char is not a vowel
s[k++] = s[i];
// skip vowel, do nothing.
}
s.resize(k); // mark the end of the string removing all vowels.
}
http://www.cplusplus.com/reference/string/string/resize/
Hi Julia,
ReplyDeleteI came across this post on your blog as I was learning on my own. I am curious as to how you come up with your solution? I'm still having some issues understanding the logic.
I also wanted to point out that you missed the reversing portion of the question but that can be fixed by calling reverse(s.begin(), s.end());
Here is my approach to things
Deletehttps://github.com/ajsingh4/RemoveVowelsFromString
I definitely wasn't aware of resize or string::npos.
My email is ajgill@ucdavis.edu
Thanks,
Gill, thank you to ask me the questions. I read your code and also read my post here as well. It is an interesting question.
ReplyDeleteTo remove vowels from a string in place, there are two things, first is to remove vowels, and the second is to stay in place. Since the string with removed vowels is shorter than original string, it is a working idea to put back in the same string starting from index 0.
To reverse the string is not asked in the problem statement: remove vowels from a string in place.
Ahhh you are correct, thank you!
ReplyDeleteI realized that you used something similar to slow-fast pointer in your approach to this.
So essentially you are putting in all of the consonants that you come across at the beginning of the string, overwriting what is there and providing you with a string of just consonants, correct?
Thank you very much for your help.