★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-husqwxth-kz.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Under a grammar given below, strings can represent a set of lowercase words. Let's use R(expr)
to denote the set of words the expression represents.git
Grammar can best be understood through simple examples:github
R("a") = {"a"}
R("w") = {"w"}
R("{a,b,c}") = {"a","b","c"}
R("{{a,b},{b,c}}") = {"a","b","c"}
(notice the final set only contains each word at most once)R("{a,b}{c,d}") = {"ac","ad","bc","bd"}
R("{a{b,c}}{{d,e},f{g,h}}") = R("{ab,ac}{dfg,dfh,efg,efh}") = {"abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh"}
Formally, the 3 rules for our grammar:express
x
, we have R(x) = {x}
e_1, e_2, ... , e_k
with k >= 2
, we have R({e_1,e_2,...}) = R(e_1) ∪ R(e_2) ∪ ...
e_1
and e_2
, we have R(e_1 + e_2) = {a + b for (a, b) in R(e_1) × R(e_2)}
, where + denotes concatenation, and × denotes the cartesian product.Given an expression
representing a set of words under the given grammar, return the sorted list of words that the expression represents. 編程
Example 1:微信
Input: "{a,b}{c{d,e}}"
Output: ["acd","ace","bcd","bce"]
Example 2:app
Input: "{{a,z},a{b,c},{ab,z}}" Output: ["a","ab","ac","z"] Explanation: Each distinct word is written only once in the final answer.
Constraints:spa
1 <= expression.length <= 50
expression[i]
consists of '{'
, '}'
, ','
or lowercase English letters.expression
represents a set of words based on the grammar given in the description.若是你熟悉 Shell 編程,那麼必定了解過花括號展開,它能夠用來生成任意字符串。code
花括號展開的表達式能夠看做一個由 花括號、逗號 和 小寫英文字母 組成的字符串,定義下面幾條語法規則:orm
x
,那麼表達式表示的字符串就只有 "x"
。
{a}
表示字符串 "a"
。{ab}
就表示字符串 "ab"
。{a,b,c}
表示字符串 "a","b","c"
。{a,b},{b,c}
也能夠表示字符串 "a","b","c"
。{a,b}{c,d}
表示字符串 "ac","ad","bc","bd"
。a{b,c,d}
表示字符串 "ab","ac","ad"
。{a{b,c}}{{d,e}f{g,h}}
能夠代換爲 {ab,ac}{dfg,dfh,efg,efh}
,表示字符串 "abdfg", "abdfh", "abefg", "abefh", "acdfg", "acdfh", "acefg", "acefh
"。給出表示基於給定語法規則的表達式 expression
,返回它所表示的全部字符串組成的有序列表。
假如你但願以「集合」的概念瞭解此題,也能夠經過點擊 「顯示英文描述」 獲取詳情。
示例 1:
輸入:"{a,b}{c{d,e}}" 輸出:["acd","ace","bcd","bce"]
示例 2:
輸入:"{{a,z}, a{b,c}, {ab,z}}" 輸出:["a","ab","ac","z"] 解釋:輸出中 不該 出現重複的組合結果。
提示:
1 <= expression.length <= 50
expression[i]
由 '{'
,'}'
,','
或小寫英文字母組成expression
用以表示一組基於題目描述中語法構造的字符串1 class Solution { 2 func braceExpansionII(_ expression: String) -> [String] { 3 var str:String = expression 4 var pos:Int = 0 5 return Array(parseRule2(&str, &pos).sorted()) 6 } 7 8 func merge(_ a:Set<String>,_ b:Set<String>) -> Set<String> 9 { 10 if a.isEmpty {return b} 11 if b.isEmpty {return a} 12 var ans:Set<String> = Set<String>() 13 for v1 in a 14 { 15 for v2 in b 16 { 17 ans.insert(v1 + v2) 18 } 19 } 20 return ans 21 } 22 23 //{a,b,c} 24 func parseRule1(_ str:inout String,_ i:inout Int) -> Set<String> 25 { 26 var ans:Set<String> = Set<String>() 27 i += 1 28 ans = parseRule2(&str, &i) 29 i += 1 30 return ans 31 } 32 33 //{a,b},{c,d} 34 func parseRule2(_ str:inout String,_ i:inout Int) -> Set<String> 35 { 36 var ans:Set<String> = Set<String>() 37 ans = parseRule3(&str, &i) 38 let arrStr:[Character] = Array(str) 39 while(i < str.count) 40 { 41 if arrStr[i] != "," {break} 42 i += 1 43 let temp:Set<String> = parseRule3(&str, &i) 44 ans = ans.union(temp) 45 } 46 return ans 47 } 48 49 //a{c,d}b{e,f} 50 func parseRule3(_ str:inout String,_ i:inout Int) -> Set<String> 51 { 52 var ans:Set<String> = Set<String>() 53 let arrStr:[Character] = Array(str) 54 while(i < str.count) 55 { 56 if arrStr[i] == "}" || arrStr[i] == "," {break} 57 if arrStr[i] == "{" 58 { 59 let temp:Set<String> = parseRule1(&str, &i) 60 ans = merge(ans, temp) 61 } 62 else 63 { 64 var temp:Set<String> = Set<String>() 65 var tmpStr:String = String() 66 while(i < str.count && arrStr[i] <= "z" && arrStr[i] >= "a") 67 { 68 tmpStr.append(arrStr[i]) 69 i += 1 70 } 71 temp.insert(tmpStr) 72 ans = merge(ans,temp) 73 } 74 } 75 return ans 76 } 77 }