CodingTour
ARTS #119

Algorithm

本周选择的算法题是:Word Break

规则

Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.

Note that the same word in the dictionary may be reused multiple times in the segmentation.

Example 1:

Input: s = "leetcode", wordDict = ["leet","code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".

Example 2:

Input: s = "applepenapple", wordDict = ["apple","pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.

Example 3:

Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
Output: false

Constraints:

  • 1 <= s.length <= 300
  • 1 <= wordDict.length <= 1000
  • 1 <= wordDict[i].length <= 20
  • s and wordDict[i] consist of only lowercase English letters.
  • All the strings of wordDict are unique.

Solution

class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        words = set(wordDict)
        dp = [False] * (len(s) + 1)
        dp[0] = True

        for i in range(1, len(s) + 1):
            for j in range(i):
                if dp[j] and s[j:i] in words:
                    dp[i] = True
                    break
        
        return dp[-1]

Review

Introduction to Event-Driven Architecture

看过的写事件驱动架构最好的文章之一。不仅对 Producer、Consumer、Broker、Stream 等概念和用法做了清晰的介绍,还把 Event 和 Log 的差异也说明白了,同时作者不局限于某领域的具体解决方案,还延伸到区域链技术,让读者能够去思考区域链中的节点和 Stream 之间的区别。

随手附上作者给出的 EDA 概览图:

img

Tip

可以在 Podfile 里通过 load 指令引入任意 ruby 脚本。

Share

组件化方案随笔: 二

上周在组件化方案随笔里提过两点:

  • 降低模块间的耦合性,并提高模块的内聚性
  • 模块独立编译、运行,对持续集成系统更友好

这其实是要求组件/模块/服务具备自治能力,能 own 属于自己的依赖,为了实现完全自治,移动端技术领域:

  • 也引入了启动项管理,业界有自注册、自启动的方案
  • 引入松耦合的通信方案,如 Router

在 iOS 里有种场景很蛋疼 — 有些事件只能由 AppDelegate 接收,如:

  • openURL:
  • 远程推送

导致依赖这些事件的组件需要开放出类似的接口供 AppDelegate 调用,这破坏了组件的自治能力,既提高了 AppDelegate 的复杂度,还有可能引起回调错乱。

如果能将 AppDelegate 这种中心化的控制器完全移除,由组件像注册启动项那样注册一个事件 Observer 或 Consumer 则能进一步提高组件的自治能力,让耦合更松散。