forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence.swift
More file actions
26 lines (23 loc) · 897 Bytes
/
LongestCommonSubsequence.swift
File metadata and controls
26 lines (23 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Question Link: https://leetcode.com/problems/longest-common-subsequence/
* Primary idea: Dynamic Programming, dp[i][j] = dp[i - 1][j - 1] + 1 or max(dp[i - 1][j], dp[i][j - 1])
*
* Time Complexity: O(mn), Space Complexity: O(1)
*/
class LongestCommonSubsequence {
func longestCommonSubsequence(_ text1: String, _ text2: String) -> Int {
let text1Chars = Array(text1), text2Chars = Array(text2)
let m = text1.count, n = text2.count
var dp = Array(repeating: Array(repeating: 0, count: n + 1), count: m + 1)
for i in 1...m {
for j in 1...n {
if text1Chars[i - 1] == text2Chars[j - 1] {
dp[i][j] = dp[i - 1][j - 1] + 1
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
}
}
}
return dp[m][n]
}
}