原文地址:http://www.javashuo.com/article/p-pcokcfwr-be.html html
Given a string s and a string t, check if s is subsequence of t.this
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).spa
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).code
Example 1:
s = "abc"
, t = "ahbgdc"
orm
Return true
.htm
Example 2:
s = "axc"
, t = "ahbgdc"
blog
Return false
.rem
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 的子序列。get
你能夠認爲 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 }