Watch 9 video solutions for Trim Trailing Vowels, a easy level problem involving String. This walkthrough by Programming Live with Larry has 157 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string s that consists of lowercase English letters.
Return the string obtained by removing all trailing vowels from s.
The vowels consist of the characters 'a', 'e', 'i', 'o', and 'u'.
Example 1:
Input: s = "idea"
Output: "id"
Explanation:
Removing "idea", we obtain the string "id".
Example 2:
Input: s = "day"
Output: "day"
Explanation:
There are no trailing vowels in the string "day".
Example 3:
Input: s = "aeiou"
Output: ""
Explanation:
Removing "aeiou", we obtain the string "".
Constraints:
1 <= s.length <= 100s consists of only lowercase English letters.Problem Overview: You are given a string and must remove all vowels that appear at the end of the string. The removal stops as soon as the first non‑vowel character is encountered. The result is the remaining prefix of the string after trimming those trailing vowels.
Approach 1: Repeated End Check (Brute Force) (Time: O(n^2), Space: O(1))
The simplest idea is to repeatedly check the last character of the string. If it is a vowel (a, e, i, o, u in either case), remove it and continue. Each removal usually creates a new substring, which can copy up to n characters depending on the language implementation. Because this trimming operation may run up to n times, the overall complexity can degrade to O(n^2). This method demonstrates the core idea clearly but is inefficient for long strings.
Approach 2: Reverse Traversal (Optimal) (Time: O(n), Space: O(1))
The efficient solution scans the string from right to left using an index. Start at the last character and keep moving left while the character is a vowel. Once a consonant or the beginning of the string is reached, stop the scan. The trimmed string is simply the substring from index 0 to the stopping position. Because each character is inspected at most once, the time complexity is O(n) with constant O(1) extra space.
This approach works well because the problem only cares about trailing characters. Reverse traversal avoids unnecessary string copies and performs a single pass over the relevant suffix. It’s a common pattern in string problems where modifications occur near the boundaries.
Approach 3: Two-Pointer Boundary Scan (Time: O(n), Space: O(1))
A variation of the reverse scan uses two pointers. One pointer starts at the end of the string and moves left while characters are vowels. The second pointer represents the fixed start of the string. After the scan stops, return the substring between the two boundaries. This technique mirrors the classic two pointers strategy used in many string processing tasks.
Recommended for interviews: Reverse traversal is the approach interviewers expect. It demonstrates that you recognize the problem only affects the suffix of the string and can be solved with a single pass and constant memory. The brute force trimming approach shows basic reasoning but wastes time with repeated substring creation, while the reverse scan delivers the optimal O(n) performance with minimal code.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Repeated End Check (Brute Force) | O(n^2) | O(1) | Simple implementation when input size is very small |
| Reverse Traversal | O(n) | O(1) | Optimal general solution for trimming trailing characters |
| Two-Pointer Boundary Scan | O(n) | O(1) | Useful when extending the problem to more complex boundary conditions |