Reverse Vowels of a String in Java with Two Pointers and HashSet

Day 87/100 🚀 | #100DaysOfDSA Solved LeetCode 345 – Reverse Vowels of a String today. Approach: Used two pointers + HashSet for fast vowel lookup. • Converted string to a char array (for in-place modification) • Maintained a HashSet of vowels (both lowercase & uppercase) • Used two pointers (left, right) Moved left forward until it finds a vowel Moved right backward until it finds a vowel • Swapped both vowels and continued Finally converted the array back to string. Time Complexity: O(n) Space Complexity: O(1) (ignoring set size as constant) Key takeaway: When checking membership frequently (like vowels), using a HashSet gives O(1) lookup, making two-pointer solutions efficient. #100DaysOfDSA #LeetCode #DSA #Java #TwoPointers #Strings #HashSet #ProblemSolving #Consistency

  • text

To view or add a comment, sign in

Explore content categories