[Swift]LeetCode1087. 字母切換 | Permutation of Letters

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-znjnclil-ma.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

A string S represents a list of words.git

Each letter in the word has 1 or more options.  If there is one option, the letter is represented as is.  If there is more than one option, then curly braces delimit the options.  For example, "{a,b,c}" represents options ["a", "b", "c"].github

For example, "{a,b,c}d{e,f}" represents the list ["ade", "adf", "bde", "bdf", "cde", "cdf"].微信

Return all words that can be formed in this manner, in lexicographical order. app

Example 1:curl

Input: "{a,b}c{d,e}f"
Output: ["acdf","acef","bcdf","bcef"] 

Example 2:ide

Input: "abcd"
Output: ["abcd"] 

Note:this

  1. 1 <= S.length <= 50
  2. There are no nested curly brackets.
  3. All characters inside a pair of consecutive opening and ending curly brackets are different.

咱們用一個特殊的字符串 S 來表示一份單詞列表,之因此能展開成爲一個列表,是由於這個字符串 S 中存在一個叫作「選項」的概念:url

單詞中的每一個字母可能只有一個選項或存在多個備選項。若是隻有一個選項,那麼該字母按原樣表示。spa

若是存在多個選項,就會以花括號包裹來表示這些選項(使它們與其餘字母分隔開),例如 "{a,b,c}" 表示 ["a", "b", "c"]

例子:"{a,b,c}d{e,f}" 能夠表示單詞列表 ["ade", "adf", "bde", "bdf", "cde", "cdf"]

請你按字典順序,返回全部以這種方式造成的單詞。 

示例 1:

輸入:"{a,b}c{d,e}f"
輸出:["acdf","acef","bcdf","bcef"]

示例 2:

輸入:"abcd"
輸出:["abcd"] 

提示:

  1. 1 <= S.length <= 50
  2. 你能夠假設題目中不存在嵌套的花括號
  3. 在一對連續的花括號(開花括號與閉花括號)之間的全部字母都不會相同

76 ms

 1 class Solution {
 2     func permute(_ S: String) -> [String] {
 3         var res:[String] = [String]()
 4         res.append(String())
 5         var cs:[Character] = Array(S)
 6         let l:Int = cs.count
 7         var i:Int = 0
 8         while(i < l)
 9         {
10             if cs[i] == "{"
11             {
12                 var t:String = String()
13                 i += 1
14                 while(cs[i] != "}")
15                 {
16                     t.append(cs[i])
17                     i += 1
18                 }
19                 let x:[String] = t.split{$0 == ","}.map(String.init)
20                 var res1:[String] = [String]()
21                 for g in x
22                 {
23                     for temp in res
24                     {
25                         res1.append(temp + g)
26                     }
27                 }
28                 res = res1
29                 
30             }
31             else
32             {
33                 var res1:[String] = [String]()
34                 for temp in res
35                 {
36                     var str = temp
37                     str.append(cs[i])
38                     res1.append(str)
39                 }
40                 res = res1
41             }
42             i += 1
43         }
44         res.sort()
45         return res
46     }
47 }
相關文章
相關標籤/搜索