Back to blog
Nov 05, 2025
6 min read

Trim Trailing Vowels

Remove all trailing vowels from a given string. Vowels include both lowercase and uppercase letters.

Difficulty: Easy | Acceptance: 77.40% | Paid: No Topics: String

Given a string s, return the string after removing all trailing vowels. A trailing vowel is a vowel that appears at the end of the string. Vowels are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ (both lowercase and uppercase).

Examples

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 <= 10⁵
s consists of lowercase and uppercase English letters.

Two Pointer Approach

Intuition Start from the end of the string and move backwards until we find a non-vowel character, then return the substring up to that point.

Steps

  • Initialize a pointer at the end of the string
  • Move the pointer backwards while the current character is a vowel
  • Return the substring from the start to the pointer position
python
class Solution:
    def trimTrailingVowels(self, s: str) -> str:
        vowels = set('aeiouAEIOU')
        i = len(s) - 1
        while i &gt;= 0 and s[i] in vowels:
            i -= 1
        return s[:i+1]

Complexity

  • Time: O(n) where n is the length of the string
  • Space: O(1) extra space (not counting the output string)
  • Notes: Simple and efficient approach with minimal space usage

Built-in Methods Approach

Intuition Use language-specific built-in methods to remove trailing characters that match a pattern.

Steps

  • Use regex or built-in string methods to remove trailing vowels
  • Return the modified string
python
import re

class Solution:
    def trimTrailingVowels(self, s: str) -> str:
        return re.sub(r'[aeiouAEIOU]+$', '', s)

Complexity

  • Time: O(n) where n is the length of the string
  • Space: O(n) for the new string (in most languages)
  • Notes: Concise code using regex, but may have slightly higher overhead

Iterative Approach

Intuition Build the result string by iterating from the end and collecting characters until we hit a non-vowel.

Steps

  • Start from the end of the string
  • Collect characters until we find a non-vowel
  • Return the result substring
python
class Solution:
    def trimTrailingVowels(self, s: str) -> str:
        vowels = set('aeiouAEIOU')
        i = len(s) - 1
        while i &gt;= 0 and s[i] in vowels:
            i -= 1
        return s[:i+1]

Complexity

  • Time: O(n) where n is the length of the string
  • Space: O(1) extra space (not counting the output string)
  • Notes: Similar to two-pointer approach but with explicit iteration