题解 | #字符串变形#
字符串变形
http://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param s string字符串
# @param n int整型
# @return string字符串
#
class Solution:
def trans(self , s: str, n: int) -> str:
# write code here
# so we need to flip the word and also lower to capital? we start the flipping from the empty string?
# step 1 loop and when it encounters space then call flipping method
# step 2 when all flipping is done. Then another parallel loop to revert the string
# string is immutable but slicable. list is mutable and slicable
# dist is muttable and slicable, tuple is immutable and slicable?
# user can fill in or concatenate string elments into the initialize empty string. But Not be able to
#change the element in the string that is already there.
#Mutable sequences can be changed after creation. Some of Python’s mutable data types are: lists, byte #arrays, sets, and dictionaries.
# immutabel after creation, strings, tuple
#https://towardsdatascience.com/immutable-vs-mutable-data-types-in-python-e8a9a6fcfbdc
ls=[0]*len(s)
for i in range(len(s)):
if s[i].islower():
ls[i]=(s[i].upper())
else:
ls[i]=(s[i].lower())
new_str=''
cur = len(ls)
for i in range(len(ls)-1,-1,-1):
if ls[i]==' ':
new_str=new_str + ''.join(ls[i+1:cur]) +' '
cur= i #
new_str = new_str + ''.join(ls[:cur])
return new_str