[Swift]LeetCode392. 判断子序列 | Is Subsequence
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10299618.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace"
is a subsequence of "abcde"
while "aec"
is not).
Example 1:
s = "abc"
, t = "ahbgdc"
Return true
.
Example 2:
s = "axc"
, t = "ahbgdc"
Return false
.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=100)。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"
是"abcde"
的一个子序列,而"aec"
不是)。
示例 1:
s = "abc"
, t = "ahbgdc"
返回 true
.
示例 2:
s = "axc"
, t = "ahbgdc"
返回 false
.
后续挑战 :
如果有大量输入的 S,称作S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?
1 class Solution { 2 func isSubsequence(_ s: String, _ t: String) -> Bool { 3 if s == "" { return true } 4 else if t == "" { return false } 5 6 var arrayS = [Character](s) 7 var arrayT = [Character](t) 8 9 func helper(arrayS: [Character], arrayT: [Character], startS: Int, startT: Int) -> Bool { 10 11 for i in startT ..< arrayT.count { 12 if arrayS[startS] == arrayT[i] { 13 if startS == arrayS.count - 1 { 14 return true 15 } else { 16 return helper(arrayS: arrayS, arrayT: arrayT, startS: startS + 1, startT: i + 1) 17 } 18 } 19 } 20 return false 21 } 22 return helper(arrayS: arrayS, arrayT: arrayT, startS: 0, startT: 0) 23 24 } 25 }
144ms
1 class Solution { 2 func isSubsequence(_ s: String, _ t: String) -> Bool { 3 var i = 0 4 var longstr = Array(t) 5 for char in s { 6 while i < longstr.count && longstr[i] != char { 7 i += 1 8 } 9 10 if i >= longstr.count { 11 return false 12 } 13 i += 1 14 } 15 return true 16 } 17 }
156ms
1 class Solution { 2 func isSubsequence(_ s: String, _ t: String) -> Bool { 3 guard s.count <= t.count else { return false } 4 5 let arrS = Array(s) 6 let arrT = Array(t) 7 8 var i = 0; 9 var j = 0; 10 11 while i < arrS.count && j < arrT.count { 12 if arrS[i] == arrT[j] { 13 i += 1 14 j += 1 15 } else { 16 j += 1 17 } 18 } 19 20 return i == arrS.count 21 } 22 }
160ms
1 class Solution { 2 func isSubsequence(_ s: String, _ t: String) -> Bool { 3 let arrS = Array(s) 4 let arrT = Array(t) 5 6 var sum = 0 7 var lastj = 0 8 for i in 0..<arrS.count { 9 for j in lastj..<arrT.count { 10 if arrS[i] == arrT[j] { 11 lastj = j+1 12 sum += 1 13 break 14 } 15 } 16 if sum <= i { 17 return false 18 } 19 } 20 return sum == arrS.count 21 } 22 }
164ms
1 class Solution { 2 func isSubsequence(_ s: String, _ t: String) -> Bool { 3 guard s != "" else { return true } 4 var i = 0 5 var j = 0 6 let s = Array(s) 7 let t = Array(t) 8 while i < s.count, j < t.count { 9 if s[i] == t[j] { 10 i += 1 11 } 12 j += 1 13 } 14 return i == s.count 15 } 16 }
192ms
1 class Solution { 2 func isSubsequence(_ s: String, _ t: String) -> Bool { 3 return isSubsequenceIteration(s, t) 4 } 5 6 func isSubsequenceIteration(_ s: String, _ t: String) -> Bool { 7 guard !s.isEmpty else { return true } 8 guard !t.isEmpty else { return false } 9 10 let sArray = Array(s) 11 let tArray = Array(t) 12 13 var hash = [String:Int]() 14 var current = 0 15 16 for character in tArray { 17 if sArray[current] == character { 18 current += 1 19 if current == sArray.count { 20 return true 21 } 22 } 23 } 24 25 return false 26 } 27 }
232ms
1 class Solution { 2 func isSubsequence(_ s: String, _ t: String) -> Bool { 3 if s.isEmpty { 4 return true 5 } 6 var result = false 7 var sIx = s.startIndex 8 var sCh = s[sIx] 9 for tCh in t { 10 if tCh == sCh { 11 // found it, go to next char 12 sIx = s.index(after: sIx) 13 if sIx == s.endIndex { 14 //found them all 15 result = true 16 //no need to keep going 17 break 18 } 19 sCh = s[sIx] 20 } 21 } 22 return result 23 } 24 }
460ms
1 class Solution { 2 func isSubsequence(_ s: String, _ t: String) -> Bool { 3 var s = Array(s) 4 s.reverse() 5 var t = Array(t) 6 t.reverse() 7 while (s.count > 0 && t.count > 0) { 8 var sLast = s[s.count-1] 9 var tLast = t[t.count-1] 10 if sLast == tLast{ 11 s.removeLast() 12 } 13 t.removeLast() 14 } 15 return s.count == 0 16 } 17 }