Skip to main content
Mention the speed
Source Link
no comment
  • 632
  • 3
  • 10

This is your mistake: if s[i] in v:

Simply change that to if s[i].lower() in vowels:. Then it easily gets accepted where I assume you do it.

The problem is that v can be large, making that check slow. For example for input s = 'a'*150000 + 'e'*150000. Your v will be the reverse, so every 'a' will take long to be found in it, as the search goes through all those 'e'. You take quadratic time and it should be linear.

Alternatively, prepare V = set(v) between the loops and use if s[i] in V:.

Bonus: a regex+slicing solution (fastest in my benchmark, fastest alternative I've found is ~27% slower):

def reverseVowels(self, s: str) -> str:
    a = re.split('([aeiouAEIOU])', s)
    a[1::2] = a[-2::-2]
    return ''.join(a)

This is your mistake: if s[i] in v:

Simply change that to if s[i].lower() in vowels:. Then it easily gets accepted where I assume you do it.

The problem is that v can be large, making that check slow. For example for input s = 'a'*150000 + 'e'*150000. Your v will be the reverse, so every 'a' will take long to be found in it, as the search goes through all those 'e'. You take quadratic time and it should be linear.

Alternatively, prepare V = set(v) between the loops and use if s[i] in V:.

Bonus: a regex+slicing solution:

def reverseVowels(self, s: str) -> str:
    a = re.split('([aeiouAEIOU])', s)
    a[1::2] = a[-2::-2]
    return ''.join(a)

This is your mistake: if s[i] in v:

Simply change that to if s[i].lower() in vowels:. Then it easily gets accepted where I assume you do it.

The problem is that v can be large, making that check slow. For example for input s = 'a'*150000 + 'e'*150000. Your v will be the reverse, so every 'a' will take long to be found in it, as the search goes through all those 'e'. You take quadratic time and it should be linear.

Alternatively, prepare V = set(v) between the loops and use if s[i] in V:.

Bonus: a regex+slicing solution (fastest in my benchmark, fastest alternative I've found is ~27% slower):

def reverseVowels(self, s: str) -> str:
    a = re.split('([aeiouAEIOU])', s)
    a[1::2] = a[-2::-2]
    return ''.join(a)
Added doc link for re.split
Source Link
no comment
  • 632
  • 3
  • 10

This is your mistake: if s[i] in v:

Simply change that to if s[i].lower() in vowels:. Then it easily gets accepted where I assume you do it.

The problem is that v can be large, making that check slow. For example for input s = 'a'*150000 + 'e'*150000. Your v will be the reverse, so every 'a' will take long to be found in it, as the search goes through all those 'e'. You take quadratic time and it should be linear.

Alternatively, prepare V = set(v) between the loops and use if s[i] in V:.

Bonus: a regex+slicingregex+slicing solution:

def reverseVowels(self, s: str) -> str:
    a = re.split('([aeiouAEIOU])', s)
    a[1::2] = a[-2::-2]
    return ''.join(a)

This is your mistake: if s[i] in v:

Simply change that to if s[i].lower() in vowels:. Then it easily gets accepted where I assume you do it.

The problem is that v can be large, making that check slow. For example for input s = 'a'*150000 + 'e'*150000. Your v will be the reverse, so every 'a' will take long to be found in it, as the search goes through all those 'e'. You take quadratic time and it should be linear.

Alternatively, prepare V = set(v) between the loops and use if s[i] in V:.

Bonus: a regex+slicing solution:

def reverseVowels(self, s: str) -> str:
    a = re.split('([aeiouAEIOU])', s)
    a[1::2] = a[-2::-2]
    return ''.join(a)

This is your mistake: if s[i] in v:

Simply change that to if s[i].lower() in vowels:. Then it easily gets accepted where I assume you do it.

The problem is that v can be large, making that check slow. For example for input s = 'a'*150000 + 'e'*150000. Your v will be the reverse, so every 'a' will take long to be found in it, as the search goes through all those 'e'. You take quadratic time and it should be linear.

Alternatively, prepare V = set(v) between the loops and use if s[i] in V:.

Bonus: a regex+slicing solution:

def reverseVowels(self, s: str) -> str:
    a = re.split('([aeiouAEIOU])', s)
    a[1::2] = a[-2::-2]
    return ''.join(a)
Added my alternative solution after all
Source Link
no comment
  • 632
  • 3
  • 10

This is your mistake: if s[i] in v:

Simply change that to if s[i].lower() in vowels:. Then it easily gets accepted where I assume you do it.

The problem is that v can be large, making that check slow. For example for input s = 'a'*150000 + 'e'*150000. Your v will be the reverse, so every 'a' will take long to be found in it, as the search goes through all those 'e'. You take quadratic time and it should be linear.

Alternatively, prepare V = set(v) between the loops and use if s[i] in V:.

Bonus: a regex+slicing solution:

def reverseVowels(self, s: str) -> str:
    a = re.split('([aeiouAEIOU])', s)
    a[1::2] = a[-2::-2]
    return ''.join(a)

This is your mistake: if s[i] in v:

Simply change that to if s[i].lower() in vowels:. Then it easily gets accepted where I assume you do it.

The problem is that v can be large, making that check slow. For example for input s = 'a'*150000 + 'e'*150000. Your v will be the reverse, so every 'a' will take long to be found in it, as the search goes through all those 'e'. You take quadratic time and it should be linear.

Alternatively, prepare V = set(v) between the loops and use if s[i] in V:.

This is your mistake: if s[i] in v:

Simply change that to if s[i].lower() in vowels:. Then it easily gets accepted where I assume you do it.

The problem is that v can be large, making that check slow. For example for input s = 'a'*150000 + 'e'*150000. Your v will be the reverse, so every 'a' will take long to be found in it, as the search goes through all those 'e'. You take quadratic time and it should be linear.

Alternatively, prepare V = set(v) between the loops and use if s[i] in V:.

Bonus: a regex+slicing solution:

def reverseVowels(self, s: str) -> str:
    a = re.split('([aeiouAEIOU])', s)
    a[1::2] = a[-2::-2]
    return ''.join(a)
Point out earlier that this is all it takes.
Source Link
no comment
  • 632
  • 3
  • 10
Loading
added 32 characters in body
Source Link
no comment
  • 632
  • 3
  • 10
Loading
Source Link
no comment
  • 632
  • 3
  • 10
Loading