题解 | #字符串变形#

字符串变形

https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e

from pickle import NONE
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @param n int整型 
# @return string字符串
#
class Solution:
    def trans(self , s: str, n: int) -> str:
        in_str=s
        out=''
        # write code here
        letter_list='abcdefghijklmnopqrstuvwxyz'
        #init pmtr
        word=[]
        stk=[]
        cnt=0
        for item in in_str:
            cnt=cnt+1
            # lower->upper, upper->lower
            if item in letter_list:
                item=item.upper()
            elif item.lower() in letter_list:
                item=item.lower()
            # record word
            if item!=' ':
                word.append(item)
            # encounter a space
            if item==' ' or cnt==n:
                if len(word)>0:
                    stk.append(''.join(word))
                    word=[]
                if item==' ':
                    stk.append(item)
        out=''.join(stk[::-1])
        return out

#面试必刷题TOP101#
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务