[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 }
相关文章
相关标签/搜索