博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode1096. 花括号展开 II | Brace Expansion II
阅读量:5281 次
发布时间:2019-06-14

本文共 4804 字,大约阅读时间需要 16 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝()
➤GitHub地址:
➤原文地址: 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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.

Grammar can best be understood through simple examples:

  • Single letters represent a singleton set containing that word.
    • R("a") = {"a"}
    • R("w") = {"w"}
  • When we take a comma delimited list of 2 or more expressions, we take the union of possibilities.
    • 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)
  • When we concatenate two expressions, we take the set of possible concatenations between two words where the first word comes from the first expression and the second word comes from the second expression.
    • 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:

  • For every lowercase letter x, we have R(x) = {x}
  • For expressions e_1, e_2, ... , e_k with k >= 2, we have R({e_1,e_2,...}) = R(e_1) ∪ R(e_2) ∪ ...
  • For expressions 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:

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:

  1. 1 <= expression.length <= 50
  2. expression[i] consists of '{''}'','or lowercase English letters.
  3. The given expression represents a set of words based on the grammar given in the description.

如果你熟悉 Shell 编程,那么一定了解过花括号展开,它可以用来生成任意字符串。

花括号展开的表达式可以看作一个由 花括号、逗号 和 小写英文字母 组成的字符串,定义下面几条语法规则:

  • 如果只给出单一的元素 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. 1 <= expression.length <= 50
  2. expression[i] 由 '{''}'',' 或小写英文字母组成
  3. 给出的表达式 expression 用以表示一组基于题目描述中语法构造的字符串

Runtime: 16 ms
Memory Usage: 13.2 MB
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
,_ b:Set
) -> Set
9 {10 if a.isEmpty {
return b}11 if b.isEmpty {
return a}12 var ans:Set
= Set
()13 for v1 in a14 {15 for v2 in b16 {17 ans.insert(v1 + v2)18 }19 }20 return ans21 }22 23 //{a,b,c}24 func parseRule1(_ str:inout String,_ i:inout Int) -> Set
25 {26 var ans:Set
= Set
()27 i += 128 ans = parseRule2(&str, &i)29 i += 130 return ans31 }32 33 //{a,b},{c,d}34 func parseRule2(_ str:inout String,_ i:inout Int) -> Set
35 {36 var ans:Set
= Set
()37 ans = parseRule3(&str, &i)38 let arrStr:[Character] = Array(str)39 while(i < str.count)40 {41 if arrStr[i] != "," { break}42 i += 143 let temp:Set
= parseRule3(&str, &i)44 ans = ans.union(temp)45 }46 return ans47 }48 49 //a{c,d}b{e,f}50 func parseRule3(_ str:inout String,_ i:inout Int) -> Set
51 {52 var ans:Set
= Set
()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
= parseRule1(&str, &i)60 ans = merge(ans, temp)61 }62 else63 {64 var temp:Set
= Set
()65 var tmpStr:String = String()66 while(i < str.count && arrStr[i] <= "z" && arrStr[i] >= "a")67 {68 tmpStr.append(arrStr[i])69 i += 170 }71 temp.insert(tmpStr)72 ans = merge(ans,temp)73 }74 }75 return ans76 }77 }

 

转载于:https://www.cnblogs.com/strengthen/p/11032169.html

你可能感兴趣的文章
MetaWeblog API Test
查看>>
移动、尺寸改变
查看>>
c# 文件笔记
查看>>
类和结构
查看>>
心得25--JDK新特性9-泛型1-加深介绍
查看>>
安装NVIDIA驱动时禁用自带nouveau驱动
查看>>
HDU-1255 覆盖的面积 (扫描线)
查看>>
Java 中 静态方法与非静态方法的区别
查看>>
Jenkins+ProGet+Windows Batch搭建全自动的内部包(NuGet)打包和推送及管理平台
查看>>
线程池的概念
查看>>
Java 序列化
查看>>
Java 时间处理实例
查看>>
Java 多线程编程
查看>>
Java 数组实例
查看>>
mysql启动过程
查看>>
2017前端面试题总结
查看>>
SWIFT国际资金清算系统
查看>>
站立会议第四天
查看>>
利用AMPScript获取Uber用户数据的访问权限
查看>>
Mysql 数据库操作
查看>>