Trim Trailing Vowels solution and explanation
8 more videos on this problem
weekly contest 491 | leetcode 3856 | leetcode 3857 | leetcode 3858 | leetcode 3859 | DSACode With Vick · 38:53 · 440 views
3856. Trim Trailing Vowels (Leetcode Easy)Programming Live with Larry · 1:55 · 192 views
Trim Trailing Vowels | LeetCode 3856 | Weekly Contest 491 | Java | Developer CoderDeveloper Coder · 3:40 · 127 views
Leetcode|3856 Trim Trailing Vowels | Java | Weekly Contest 491Cakot Coding · 4:52 · 78 views
LeetCode Problem 3856 | Trim Trailing VowelsRepovive TV · 2:33 · 72 views
Leetcode 3856 : Trim Trailing VowelsShubham Bansal · 39:08 · 46 views
Leetcode 3856 | Trim Trailing Vowels | Weekly Contest 491Code Like Him · 1:45 · 29 views
Leetcode Weekly Contest 491 Q1. Trim Trailing Vowels #python #dsaADevOpsEngineer · 1:56 · 16 views
3856. Trim Trailing Vowels | Leetcode 491 | Leetcode TodayOptimized Concepts · 3:03 · 12 views
The problem
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 <= 100sconsists of only lowercase English letters.
Approach overview
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.
Solution
We traverse the string from the end in reverse order until we encounter the first non-vowel character. Then we return the substring from the beginning of the string up to that position.
The time complexity is O(n), where n is the length of the string. The space complexity is O(1).
class Solution:
def trimTrailingVowels(self, s: str) -> str:
i = len(s) - 1
while i >= 0 and s[i] in "aeiou":
i -= 1
return s[: i + 1]class Solution {
public String trimTrailingVowels(String s) {
int i = s.length() - 1;
while (i >= 0 && "aeiou".indexOf(s.charAt(i)) != -1) {
i--;
}
return s.substring(0, i + 1);
}
}class Solution {
public:
string trimTrailingVowels(string s) {
int i = s.size() - 1;
while (i >= 0 && string("aeiou").find(s[i]) != string::npos) {
i--;
}
return s.substr(0, i + 1);
}
};function trimTrailingVowels(s: string): string {
let i = s.length - 1;
while (i >= 0 && 'aeiou'.indexOf(s[i]) !== -1) {
i--;
}
return s.slice(0, i + 1);
}func trimTrailingVowels(s string) string {
i := len(s) - 1
for i >= 0 && strings.IndexByte("aeiou", s[i]) != -1 {
i--
}
return s[:i+1]
}Complexity analysis
| 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 |
Frequently asked questions
Is Trim Trailing Vowels easy or hard?
Trim Trailing Vowels is considered an easy problem. It focuses on basic string traversal and boundary conditions rather than complex data structures or algorithms.
Trim Trailing Vowels Python/Java solution
In Python or Java, iterate from the last character of the string while checking if the character belongs to a vowel set. Once the scan stops, return the substring from index 0 to the final pointer. This keeps the runtime O(n) and avoids repeated string copying.
How to solve Trim Trailing Vowels in O(n)?
Start scanning from the last index of the string. While the character is a vowel (a, e, i, o, u in either case), decrement the index. Once a non‑vowel appears, return the substring from the start of the string up to that position.
What is the best approach for Trim Trailing Vowels?
Reverse traversal from the end of the string is the most efficient method. Move a pointer left while characters are vowels, then return the substring before that point. This solution runs in O(n) time and uses O(1) extra space.
Is Trim Trailing Vowels asked at Google/Amazon/Meta?
String manipulation problems like trimming characters from boundaries appear frequently in coding interviews at companies such as Amazon and Google. Variations often test reverse traversal, two‑pointer scanning, or efficient substring handling.
What data structure is used in Trim Trailing Vowels?
The problem mainly uses basic string processing. A pointer or index is used to traverse the string from the end, and a constant lookup for vowels determines whether to continue trimming.
What is the time complexity of Trim Trailing Vowels?
The optimal solution runs in O(n) time where n is the length of the string. In the worst case every character must be checked when all characters are vowels. Space complexity remains O(1) because only an index variable is used.
