[Swift]LeetCode1078. Bigram 分詞 | Occurrences After Bigram

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-volphfnj-mb.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given words first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second.git

For each such occurrence, add "third" to the answer, and return the answer.github

Example 1:微信

Input: text = "alice is a good girl she is a good student", first = "a", second = "good" Output: ["girl","student"] 

Example 2:app

Input: text = "we will we will rock you", first = "we", second = "will" Output: ["we","rock"] 

Note:ide

  1. 1 <= text.length <= 1000
  2. text consists of space separated words, where each word consists of lowercase English letters.
  3. 1 <= first.length, second.length <= 10
  4. first and second consist of lowercase English letters.

給出第一個詞 first 和第二個詞 second,考慮在某些文本 text 中可能以 "first second third" 形式出現的狀況,其中 second 緊隨 first 出現,third 緊隨 second 出現。spa

對於每種這樣的狀況,將第三個詞 "third" 添加到答案中,並返回答案。 code

示例 1:component

輸入:text = "alice is a good girl she is a good student", first = "a", second = "good"
輸出:["girl","student"]

示例 2:orm

輸入:text = "we will we will rock you", first = "we", second = "will"
輸出:["we","rock"] 

提示:

  1. 1 <= text.length <= 1000
  2. text 由一些用空格分隔的單詞組成,每一個單詞都由小寫英文字母組成
  3. 1 <= first.length, second.length <= 10
  4. first 和 second 由小寫英文字母組成

4ms
 1 class Solution {
 2     func findOcurrences(_ text: String, _ first: String, _ second: String) -> [String] {
 3       let subString = first + " " + second
 4       let arr = text.split(separator: " ")
 5       var result = [String]()
 6       for i in 0..<arr.count-2 {
 7         if arr[i] == first && arr[i+1] == second {
 8           result.append(String(arr[i+2]))
 9         }
10       }
11       return result
12     }
13 }

Runtime: 8 ms

Memory Usage: 21.7 MB
 1 import Foundation
 2 class Solution {
 3     func findOcurrences(_ text: String, _ first: String, _ second: String) -> [String] {
 4         if text.isEmpty {return []}
 5         var words:[String] = text.components(separatedBy: " ")
 6         var list:[String] = [String]()
 7         for i in 2..<words.count
 8         {
 9             if first == words[i-2] && second == words[i-1]
10             {
11                 list.append(words[i])
12             }
13         }
14         return list
15     }
16 }

8ms

 1 class Solution {
 2     func findOcurrences(_ text: String, _ first: String, _ second: String) -> [String] {
 3         var answer: [String] = []
 4         
 5         var words = Array(text.split(separator: " "))
 6         
 7         var i = 0
 8                 
 9         while (i + 2) < words.count {
10             if words[i] == first, words[i + 1] == second {
11                 answer.append(String(words[i + 2]))
12             }
13             
14             i += 1
15         }
16 
17         return answer
18     }
19 }
相關文章
相關標籤/搜索