Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Certainly! Below is a C++ solution for the LeetCode problem 345, "Reverse Vowels of a String". This solution reverses the vowels in a given string while keeping the consonants in their original positions.
- C++ Code:
- #include <iostream>
- #include <string>
- #include <unordered_set>
- class Solution {
- public:
- std::string reverseVowels(std::string s) {
- std::unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
- int left = 0, right = s.size() - 1;
- while (left < right) {
- // Move left pointer to the next vowel
- while (left < right && vowels.find(s[left]) == vowels.end()) {
- left++;
- }
- // Move right pointer to the previous vowel
- while (left < right && vowels.find(s[right]) == vowels.end()) {
- right--;
- }
- // Swap the vowels
- if (left < right) {
- std::swap(s[left], s[right]);
- left++;
- right--;
- }
- }
- return s;
- }
- };
- // Example usage
- int main() {
- Solution solution;
- std::string input = "hello";
- std::string output = solution.reverseVowels(input);
- std::cout << "Reversed vowels string: " << output << std::endl; // Output: "holle"
- input = "leetcode";
- output = solution.reverseVowels(input);
- std::cout << "Reversed vowels string: " << output << std::endl; // Output: "leotcede"
- return 0;
- }
- Explanation:
- Vowel Set: We use an unordered_set to store the vowels for quick lookup.
- Two-Pointer Technique:
- We initialize two pointers, left (starting from the beginning) and right (starting from the end).
- The left pointer moves to the right until it finds a vowel, and the right pointer moves to the left until it finds a vowel.
- Swapping: If both pointers point to vowels, we swap them and move both pointers inward.
- Loop: The loop continues until the left pointer is no longer less than the right pointer.
- Example Usage: In the main function, we test the reverseVowels method with different strings and print the results.
- Example Output:
- For the input "hello", the output will be:
- Reversed vowels string: holle
- For the input "leetcode", the output will be:
- Reversed vowels string: leotcede
- You can modify the input strings in the example usage to test with different cases!
- Certainly! Below is a C# solution for the LeetCode problem 345, "Reverse Vowels of a String". This solution reverses the vowels in a given string while keeping the consonants in their original positions.
- C# Code:
- using System;
- using System.Collections.Generic;
- public class Solution {
- public string ReverseVowels(string s) {
- HashSet<char> vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
- char[] charArray = s.ToCharArray();
- int left = 0, right = charArray.Length - 1;
- while (left < right) {
- // Move left pointer to the next vowel
- while (left < right && !vowels.Contains(charArray[left])) {
- left++;
- }
- // Move right pointer to the previous vowel
- while (left < right && !vowels.Contains(charArray[right])) {
- right--;
- }
- // Swap the vowels
- if (left < right) {
- char temp = charArray[left];
- charArray[left] = charArray[right];
- charArray[right] = temp;
- left++;
- right--;
- }
- }
- return new string(charArray);
- }
- }
- // Example usage
- public class Program {
- public static void Main() {
- Solution solution = new Solution();
- string input1 = "hello";
- string output1 = solution.ReverseVowels(input1);
- Console.WriteLine("Reversed vowels string: " + output1); // Output: "holle"
- string input2 = "leetcode";
- string output2 = solution.ReverseVowels(input2);
- Console.WriteLine("Reversed vowels string: " + output2); // Output: "leotcede"
- }
- }
- Explanation:
- Vowel Set: We use a HashSet<char> to store the vowels for quick lookup.
- Character Array: The input string is converted to a character array for easier manipulation.
- Two-Pointer Technique:
- Two pointers, left (starting from the beginning) and right (starting from the end), are used to find and swap vowels.
- The left pointer moves to the right until it finds a vowel, and the right pointer moves to the left until it finds a vowel.
- Swapping: If both pointers point to vowels, we swap them and move both pointers inward.
- Loop: The loop continues until the left pointer is no longer less than the right pointer.
- Example Usage: In the Main method, we test the ReverseVowels method with different strings and print the results.
- Example Output:
- For the input "hello", the output will be:
- Reversed vowels string: holle
- For the input "leetcode", the output will be:
- Reversed vowels string: leotcede
- You can modify the input strings in the example usage to test with different cases!
- Sure! Below is a Python solution for the LeetCode problem 345, "Reverse Vowels of a String". This solution reverses the vowels in a given string while keeping the consonants in their original positions.
- Python Code:
- class Solution:
- def reverseVowels(self, s: str) -> str:
- vowels = set('aeiouAEIOU') # Set of vowels for quick lookup
- s_list = list(s) # Convert the string to a list for mutability
- left, right = 0, len(s) - 1 # Two pointers
- while left < right:
- # Move left pointer to the next vowel
- while left < right and s_list[left] not in vowels:
- left += 1
- # Move right pointer to the previous vowel
- while left < right and s_list[right] not in vowels:
- right -= 1
- # Swap the vowels
- if left < right:
- s_list[left], s_list[right] = s_list[right], s_list[left]
- left += 1
- right -= 1
- return ''.join(s_list) # Join the list back into a string
- # Example usage
- if __name__ == "__main__":
- solution = Solution()
- input1 = "hello"
- output1 = solution.reverseVowels(input1)
- print("Reversed vowels string:", output1) # Output: "holle"
- input2 = "leetcode"
- output2 = solution.reverseVowels(input2)
- print("Reversed vowels string:", output2) # Output: "leotcede"
- Explanation:
- Vowel Set: A set of vowels is created for quick lookup.
- List Conversion: The input string is converted to a list to allow for mutable operations.
- Two-Pointer Technique:
- Two pointers, left and right, are initialized at the start and end of the list.
- The left pointer moves to the right until it finds a vowel, while the right pointer moves to the left until it finds a vowel.
- Swapping: If both pointers point to vowels, they are swapped, and both pointers are moved inward.
- Loop: This process continues until the left pointer is no longer less than the right pointer.
- Joining: Finally, the list is joined back into a string and returned.
- Example Output:
- For the input "hello", the output will be:
- Reversed vowels string: holle
- For the input "leetcode", the output will be:
- Reversed vowels string: leotcede
- You can modify the input strings in the example usage to test with different cases!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement