Advertisement
LikeRampage

C++/C#/Python leetcode 345. Reverse Vowels of a String

Nov 17th, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.34 KB | Software | 0 0
  1. 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.
  2.  
  3. C++ Code:
  4. #include <iostream>
  5. #include <string>
  6. #include <unordered_set>
  7.  
  8. class Solution {
  9. public:
  10.     std::string reverseVowels(std::string s) {
  11.         std::unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
  12.         int left = 0, right = s.size() - 1;
  13.  
  14.         while (left < right) {
  15.             // Move left pointer to the next vowel
  16.             while (left < right && vowels.find(s[left]) == vowels.end()) {
  17.                 left++;
  18.             }
  19.             // Move right pointer to the previous vowel
  20.             while (left < right && vowels.find(s[right]) == vowels.end()) {
  21.                 right--;
  22.             }
  23.             // Swap the vowels
  24.             if (left < right) {
  25.                 std::swap(s[left], s[right]);
  26.                 left++;
  27.                 right--;
  28.             }
  29.         }
  30.  
  31.         return s;
  32.     }
  33. };
  34.  
  35. // Example usage
  36. int main() {
  37.     Solution solution;
  38.     std::string input = "hello";
  39.     std::string output = solution.reverseVowels(input);
  40.     std::cout << "Reversed vowels string: " << output << std::endl; // Output: "holle"
  41.  
  42.     input = "leetcode";
  43.     output = solution.reverseVowels(input);
  44.     std::cout << "Reversed vowels string: " << output << std::endl; // Output: "leotcede"
  45.  
  46.     return 0;
  47. }
  48. Explanation:
  49. Vowel Set: We use an unordered_set to store the vowels for quick lookup.
  50. Two-Pointer Technique:
  51. We initialize two pointers, left (starting from the beginning) and right (starting from the end).
  52. 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.
  53. Swapping: If both pointers point to vowels, we swap them and move both pointers inward.
  54. Loop: The loop continues until the left pointer is no longer less than the right pointer.
  55. Example Usage: In the main function, we test the reverseVowels method with different strings and print the results.
  56. Example Output:
  57. For the input "hello", the output will be:
  58.  
  59. Reversed vowels string: holle
  60. For the input "leetcode", the output will be:
  61.  
  62. Reversed vowels string: leotcede
  63. You can modify the input strings in the example usage to test with different cases!
  64.  
  65. 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.
  66.  
  67. C# Code:
  68. using System;
  69. using System.Collections.Generic;
  70.  
  71. public class Solution {
  72.     public string ReverseVowels(string s) {
  73.         HashSet<char> vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
  74.         char[] charArray = s.ToCharArray();
  75.         int left = 0, right = charArray.Length - 1;
  76.  
  77.         while (left < right) {
  78.             // Move left pointer to the next vowel
  79.             while (left < right && !vowels.Contains(charArray[left])) {
  80.                 left++;
  81.             }
  82.             // Move right pointer to the previous vowel
  83.             while (left < right && !vowels.Contains(charArray[right])) {
  84.                 right--;
  85.             }
  86.             // Swap the vowels
  87.             if (left < right) {
  88.                 char temp = charArray[left];
  89.                 charArray[left] = charArray[right];
  90.                 charArray[right] = temp;
  91.                 left++;
  92.                 right--;
  93.             }
  94.         }
  95.  
  96.         return new string(charArray);
  97.     }
  98. }
  99.  
  100. // Example usage
  101. public class Program {
  102.     public static void Main() {
  103.         Solution solution = new Solution();
  104.        
  105.         string input1 = "hello";
  106.         string output1 = solution.ReverseVowels(input1);
  107.         Console.WriteLine("Reversed vowels string: " + output1); // Output: "holle"
  108.  
  109.         string input2 = "leetcode";
  110.         string output2 = solution.ReverseVowels(input2);
  111.         Console.WriteLine("Reversed vowels string: " + output2); // Output: "leotcede"
  112.     }
  113. }
  114. Explanation:
  115. Vowel Set: We use a HashSet<char> to store the vowels for quick lookup.
  116. Character Array: The input string is converted to a character array for easier manipulation.
  117. Two-Pointer Technique:
  118. Two pointers, left (starting from the beginning) and right (starting from the end), are used to find and swap vowels.
  119. 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.
  120. Swapping: If both pointers point to vowels, we swap them and move both pointers inward.
  121. Loop: The loop continues until the left pointer is no longer less than the right pointer.
  122. Example Usage: In the Main method, we test the ReverseVowels method with different strings and print the results.
  123. Example Output:
  124. For the input "hello", the output will be:
  125.  
  126. Reversed vowels string: holle
  127. For the input "leetcode", the output will be:
  128.  
  129. Reversed vowels string: leotcede
  130. You can modify the input strings in the example usage to test with different cases!
  131.  
  132.  
  133. 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.
  134.  
  135. Python Code:
  136. class Solution:
  137.     def reverseVowels(self, s: str) -> str:
  138.         vowels = set('aeiouAEIOU')  # Set of vowels for quick lookup
  139.         s_list = list(s)  # Convert the string to a list for mutability
  140.         left, right = 0, len(s) - 1  # Two pointers
  141.  
  142.         while left < right:
  143.             # Move left pointer to the next vowel
  144.             while left < right and s_list[left] not in vowels:
  145.                 left += 1
  146.             # Move right pointer to the previous vowel
  147.             while left < right and s_list[right] not in vowels:
  148.                 right -= 1
  149.             # Swap the vowels
  150.             if left < right:
  151.                 s_list[left], s_list[right] = s_list[right], s_list[left]
  152.                 left += 1
  153.                 right -= 1
  154.  
  155.         return ''.join(s_list)  # Join the list back into a string
  156.  
  157.  
  158. # Example usage
  159. if __name__ == "__main__":
  160.     solution = Solution()
  161.    
  162.     input1 = "hello"
  163.     output1 = solution.reverseVowels(input1)
  164.     print("Reversed vowels string:", output1)  # Output: "holle"
  165.  
  166.     input2 = "leetcode"
  167.     output2 = solution.reverseVowels(input2)
  168.     print("Reversed vowels string:", output2)  # Output: "leotcede"
  169. Explanation:
  170. Vowel Set: A set of vowels is created for quick lookup.
  171. List Conversion: The input string is converted to a list to allow for mutable operations.
  172. Two-Pointer Technique:
  173. Two pointers, left and right, are initialized at the start and end of the list.
  174. 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.
  175. Swapping: If both pointers point to vowels, they are swapped, and both pointers are moved inward.
  176. Loop: This process continues until the left pointer is no longer less than the right pointer.
  177. Joining: Finally, the list is joined back into a string and returned.
  178. Example Output:
  179. For the input "hello", the output will be:
  180.  
  181. Reversed vowels string: holle
  182. For the input "leetcode", the output will be:
  183.  
  184. Reversed vowels string: leotcede
  185. You can modify the input strings in the example usage to test with different cases!
  186.  
Tags: C++ C# python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement