[Python] How can i improve my python code of leetcode problem 345 and how i can i improve the...

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    • Task of the problem:

    Given a string s, reverse only all the vowels in the string and return it.

    The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.

    Example 1:

    Input: s = "IceCreAm"

    Output: "AceCreIm"

    Explanation:

    The vowels in s are ['I', 'e', 'e', 'A']. On reversing the vowels, s becomes "AceCreIm".

    class Solution:
    def reverseVowels(self, s: str) -> str:
    vowels = ['a', 'A', 'e', 'E', 'i', 'I', 'O', 'o', 'U','u']
    found = []
    location = []
    for i in range(len(s)):
    if s in vowels:
    found.append(s)
    location.append(i)
    location.reverse()
    r2 = []
    for i in range(len(s)):
    if i in location:
    r2.append(found[location.index(i)])
    else: r2.append(s)
    s = ''.join(r2)
    return(s)


    This code passes all test cases except the last one (479/480 test cases passed) the last one is very large but i have attached it below.

    [​IMG]

    how can i improve the time complexity of my code and how can i make it faster? https://www.mashupstack.com/share/67cd359de9836

    Continue reading...

Compartilhe esta Página