Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Python: Better approach to switch Case

k

Jun 21, 2023

Given Approach:

#convert string to list of characters

chs = list(string) # if the current char is in upper case, change it to lower case or vice versa chs[i] = chs[i].swapcase() string = ''.join(chs))

Suggested Approach:

  • This will be much faster execution
  • Quick to wirte.

string = string[:j] + string[j].swapcase() + string[j+1:]

1

0

Comments
Comments