• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode97.交错字符串|InterleavingString

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given s1s2s3, find whether s3 is formed by the interleaving of s1 and s2.

Example 1:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true

Example 2:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false

给定三个字符串 s1s2s3, 验证 s3 是否是由 s1 和 s2 交错组成的。

示例 1:

输入: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
输出: true

示例 2:

输入: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
输出: false

12ms
 1 class Solution {
 2     func isInterleave(_ s1: String, _ s2: String, _ s3: String) -> Bool {
 3         var cache = [[Int]](repeating: [Int](repeating: -1, count: s2.count), count: s1.count)
 4         return is_Interleave(s1, 0, s2, 0, s3, 0, &cache)
 5     }
 6 
 7     fileprivate func is_Interleave(_ s1: String, _ i: Int, _ s2: String, _ j: Int, _ s3: String, _ k: Int, _ cache: inout [[Int]]) -> Bool {
 8         if i == s1.count {
 9             return s2[j..<s2.count] == s3[k..<s3.count]
10         }
11         if j == s2.count {
12             return s1[i..<s1.count] == s3[k..<s3.count]
13         }
14         if cache[i][j] >= 0 {
15             return cache[i][j] == 1 ? true : false
16         }
17 
18         var ans = false
19 
20         if (s3[k] == s1[i] && is_Interleave(s1, i + 1, s2, j, s3, k + 1, &cache)) ||
21             (s3[k] == s2[j] && is_Interleave(s1, i, s2, j + 1, s3, k + 1, &cache)) {
22             ans = true
23         }
24         cache[i][j] = ans ? 1 : 0
25 
26         return ans
27     }
28 }
29 
30 extension String {
31     subscript (i: Int) -> Character {
32         return self[index(startIndex, offsetBy: i)]
33     }
34     
35     subscript(_ range: CountableRange<Int>) -> String {
36         let idx1 = index(startIndex, offsetBy: max(0, range.lowerBound))
37         let idx2 = index(startIndex, offsetBy: min(self.count, range.upperBound))
38         return String(self[idx1..<idx2])
39     }
40 }

12ms

 1 class Solution {
 2     func isInterleave(_ s1: String, _ s2: String, _ s3: String) -> Bool {
 3         if s1.count + s2.count != s3.count {
 4             return false
 5         }
 6         var dp: [[Bool]] = Array<Array<Bool>>(repeating: Array<Bool>(repeating: false, count: s2.count+1), count: s1.count+1)
 7         for i in 0 ..< s1.count + 1{
 8             for j in 0 ..< s2.count + 1{
 9                 if i == 0 && j == 0 {
10                     dp[0][0] = true
11                 } else if i == 0 {
12                     dp[0][j] = dp[0][j-1] && (s2[j-1] == s3[j-1])
13                 } else if j == 0 {
14                     dp[i][0] = dp[i-1][0] && (s1[i-1] == s3[i-1])
15                 } else {
16                     dp[i][j] = (dp[i][j-1] && (s2[j-1] == s3[i+j-1])) || (dp[i-1][j] && (s1[i-1] == s3[i+j-1]))
17                 }
18             }
19         }
20         return dp[s1.count][s2.count]
21     }
22 }
23 
24 extension String {
25     subscript(index: Int) -> Character {
26         return self[self.index(self.startIndex, offsetBy: index)]
27     }
28 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
使用Swift制作一个新闻通知中心插件(2)发布时间:2022-07-13
下一篇:
iOS8使用CoreGraphics实现渐变效果-Swift基础教程发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap