diff --git a/README.md b/README.md index 8eb0c6e5f..16acd1391 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ LeetCode | # | Title | Solution | Difficulty | |---| ----- | -------- | ---------- | +|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | [C++](./algorithms/cpp/reverseVowelsOfAString/reverseVowelsOfAString.cpp)|Easy| |337|[House Robber III](https://leetcode.com/problems/house-robber-iii/) | [C++](./algorithms/cpp/houseRobber/houseRobberIII.cpp)|Medium| |334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [C++](./algorithms/cpp/increasingTripletSubsequence/increasingTripletSubsequence.cpp)|Medium| |330|[Patching Array](https://leetcode.com/problems/patching-array/) | [C++](./algorithms/cpp/patchingArray/PatchingArray.cpp)|Medium| diff --git a/algorithms/cpp/reverseVowelsOfAString/reverseVowelsOfAString.cpp b/algorithms/cpp/reverseVowelsOfAString/reverseVowelsOfAString.cpp new file mode 100644 index 000000000..f4f63922a --- /dev/null +++ b/algorithms/cpp/reverseVowelsOfAString/reverseVowelsOfAString.cpp @@ -0,0 +1,50 @@ +// Source : https://leetcode.com/problems/reverse-vowels-of-a-string/ +// Author : Calinescu Valentin +// Date : 2016-04-30 + +/*************************************************************************************** + * + * Write a function that takes a string as input and reverse only the vowels of a + * string. + * + * Example 1: + * Given s = "hello", return "holle". + * + * Example 2: + * Given s = "leetcode", return "leotcede". + * + ***************************************************************************************/ +class Solution { +public: + string reverseVowels(string s) { + list vowels; + set vows; + vows.insert('a'); + vows.insert('A'); + vows.insert('e'); + vows.insert('E'); + vows.insert('i'); + vows.insert('I'); + vows.insert('o'); + vows.insert('O'); + vows.insert('u'); + vows.insert('U'); + string result; + for(int i = 0; i < s.size(); i++) + { + if(vows.find(s[i]) != vows.end()) + vowels.push_back(s[i]); + } + for(int i = 0; i < s.size(); i++) + { + if(vows.find(s[i]) != vows.end()) + { + result.push_back(vowels.back()); + vowels.pop_back(); + } + else + result.push_back(s[i]); + } + return result; + } +};