Skip to main content

Trim Trailing Vowels - Solution & Explanation

EasyString5 min read
Practice this problem

Problem Statement

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 <= 100
  • s consists 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).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Repeated End Check (Brute Force)O(n^2)O(1)Simple implementation when input size is very small
Reverse TraversalO(n)O(1)Optimal general solution for trimming trailing characters
Two-Pointer Boundary ScanO(n)O(1)Useful when extending the problem to more complex boundary conditions

Video Solution

weekly contest 491 | leetcode 3856 | leetcode 3857 | leetcode 3858 | leetcode 3859 | DSACode With Vick440 views views

Watch 8 more video solutions →

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.

Ready to solve this problem?

Practice Trim Trailing Vowels with our built-in code editor and test cases.

Practice on FleetCode