【Leetcode】784. Letter Case Permutation

Given a string S, we can transform every letter individually?to be lowercase or uppercase to create another string.? Return a list of all possible strings we could create.

class Solution(object):

? ? def letterCasePermutation(self, S):

? ? ? ? """

? ? ? ? :type S: str

? ? ? ? :rtype: List[str]

? ? ? ? """

? ? ? ? if not S: return ['']

? ? ? ? res = ['']

? ? ? ? for s in S:

? ? ? ? ? ? if s.isalpha():

? ? ? ? ? ? ? ? res = [i+j for i in res for j in [s.upper(), s.lower()]]

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? res = [i+s for i in res]

? ? ? ? return res

1 判斷是否是字母,用str.isalpha()

2 初始化一個(gè)list=[],但由于list中的element是字符串,所以初始化寫(xiě)成['']的形式,代表初始化為以字符串為element的list

3 一個(gè)字符一個(gè)字符地判斷,將此字符前的所有字符的所有permutation都寫(xiě)到res中,當(dāng)判斷下一個(gè)字符時(shí),只需要將之前所有組合+當(dāng)前字符的形式再做一次permutation就可以了

4?if not S: return [''] 也是要返回成這樣才行

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,181評(píng)論 0 3
  • 各校歷年復(fù)試機(jī)試試題 清華、北大、華科試題詳細(xì)筆記部分,少筆記部分與少數(shù)leetcode【含個(gè)人整理筆記】 一、詳...
    AIM外星人閱讀 1,327評(píng)論 0 1
  • 原題鏈接原題大意:給定字符串s,字符串中每個(gè)字母的大小寫(xiě)不同可組成新的字符串,求出可能組成的全部字符串。下面是給出...
    敢想敢做_閱讀 467評(píng)論 0 0
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,142評(píng)論 0 2
  • 朋友說(shuō),這是一個(gè)適合浪的城市…… (一) 他從座位上起身,回頭對(duì)著地鐵運(yùn)行線路圖看來(lái)看去,從運(yùn)行的一端數(shù)到另一端,...
    惠小梨閱讀 626評(píng)論 0 0

友情鏈接更多精彩內(nèi)容