Skip to content

Commit 875eb7b

Browse files
committed
feat: add 1143. Longest Common Subsequence
1 parent ba9889b commit 875eb7b

19 files changed

+118
-56
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
src/.DS_Store

.idea/codeStyles/Project.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/kotlinc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/KotlinJavaRuntime.xml

Lines changed: 16 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,30 @@
1616
LeetCode is the best platform to help you enhance your skills, expand your knowledge and prepare for technical
1717
interviews.
1818

19-
https://leetcode.com/nphau
19+
https://leetcode.com/nphausg
2020

2121
🔥 Easy 🔥🔥 Medium 🔥🔥🔥 Hard
2222

2323
## 💎 Coding Challenge
2424

25-
| Challenge | Level | Java | Kotlin | Link
26-
|---|---|---|---|---
27-
| ValidParentheses | 🔥 | [Solution](src/sg/nphau/leetcode/easy/ValidParentheses.java) | - | [20](https://leetcode.com/problems/valid-parentheses)
28-
| Remove Duplicates | 🔥 | [Solution](src/sg/nphau/leetcode/easy/RemoveDuplicates.java) | - | [26](https://leetcode.com/problems/remove-duplicates-from-sorted-array)
29-
| Remove Element | 🔥 | [Solution](src/sg/nphau/leetcode/easy/RemoveElement.java) | - | [27](https://leetcode.com/problems/remove-element)
30-
| Concatenation Of Array | 🔥 | [Solution](src/sg/nphau/leetcode/easy/ConcatenationArray.java) | - | [1929](https://leetcode.com/problems/concatenation-of-array)
31-
| Letter Combinations of a Phone Number | 🔥🔥 | [Solution](src/sg/nphau/leetcode/medium/LetterCombinations.java) | - | [17](https://leetcode.com/problems/letter-combinations-of-a-phone-number)
32-
| Decode Ways | 🔥🔥 | [Solution](src/sg/nphau/leetcode/medium/DecodeWays.java) | - | [91](https://leetcode.com/problems/decode-ways)
33-
| Find K Closest Elements | 🔥🔥 | [Solution](src/sg/nphau/leetcode/medium/FindClosestElements.java) | - | [658](https://leetcode.com/problems/find-k-closest-elements)
34-
| First Missing Positive | 🔥🔥🔥 | [Solution](src/sg/nphau/leetcode/hard/FirstMissingPositive.java) | [Solution](src/sg/nphau/leetcode/hard/FirstMissingPositive.kt) | [41](https://leetcode.com/problems/first-missing-positive)
25+
| Challenge | Level | Java | Kotlin |
26+
|------------------------------------------------------------------------------------------------------------------|--------|---------------------------------------------------------------------------|-------------------------------------------------------------------|
27+
| [20. ValidParentheses](https://leetcode.com/problems/valid-parentheses) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ValidParentheses.java) | - |
28+
| [26. Remove Duplicates](https://leetcode.com/problems/remove-duplicates-from-sorted-array) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/RemoveDuplicates.java) | - |
29+
| [27. Remove Element](https://leetcode.com/problems/remove-element) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/RemoveElement.java) | - |
30+
| [1929. Concatenation Of Array](https://leetcode.com/problems/concatenation-of-array) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ConcatenationArray.java) | - |
31+
| [17. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/LetterCombinations.java) | - |
32+
| [91. Decode Ways ](https://leetcode.com/problems/decode-ways) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/DecodeWays.java) | - |
33+
| [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/LongestCommonSubsequence.java) |
34+
| [658. Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/FindClosestElements.java) | - |
35+
| [41. First Missing Positive](https://leetcode.com/problems/first-missing-positive) | 🔥🔥🔥 | [Solution](src/com/nphausg/leetcode/hard/FirstMissingPositive.java) | [Solution](src/com/nphausg/leetcode/hard/FirstMissingPositive.kt) |
3536

3637
![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png)
3738

3839
## 👇 Author
3940

4041
<p>
4142
<a href="https://nphausg.medium.com/" target="_blank">
42-
<img src="https://avatars2.githubusercontent.com/u/13111806?s=400&u=f09b6160dbbe2b7eeae0aeb0ab4efac0caad57d7&v=4" width="96" height="96">
43+
<img src="https://avatars2.githubusercontent.com/u/13111806?s=400&u=f09b6160dbbe2b7eeae0aeb0ab4efac0caad57d7&v=4" width="96" height="96" alt="">
4344
</a>
4445
</p>
Binary file not shown.

src/com/.DS_Store

6 KB
Binary file not shown.

src/com/nphausg/leetcode/Main.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Created by nphau on 09/12/2021, 00:30
3+
* Copyright (c) 2021 . All rights reserved.
4+
* Last modified 09/12/2021, 15:54
5+
*/
6+
package com.nphausg.leetcode;
7+
8+
import com.nphausg.leetcode.medium.LongestCommonSubsequence;
9+
10+
public class Main {
11+
12+
public static void main(String[] args) {
13+
14+
// Test 1: 3
15+
String text1 = "abcde";
16+
String text2 = "ace";
17+
18+
// Test 2: 3
19+
// String text1 = "abc";
20+
// String text2 = "abc";
21+
22+
// Test 3: 0
23+
// String text1 = "abc";
24+
// String text2 = "def";
25+
26+
System.out.println(LongestCommonSubsequence.longestCommonSubsequence(text1, text2));
27+
}
28+
}

src/sg/nphau/leetcode/easy/ConcatenationArray.java renamed to src/com/nphausg/leetcode/easy/ConcatenationArray.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* Copyright (c) 2021 . All rights reserved.
44
* Last modified 09/12/2021, 15:54
55
*/
6-
package sg.nphau.leetcode.easy;
6+
package com.nphausg.leetcode.easy;
77

88
/**
99
* Given an integer array nums of length n, you want to create an array ans of length 2n
1010
* where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed).
1111
* Specifically, ans is the concatenation of two nums arrays.
1212
* Return the array ans.
13-
* https://leetcode.com/problems/concatenation-of-array/
13+
* <a href="https://leetcode.com/problems/concatenation-of-array/">...</a>
1414
*/
1515
public class ConcatenationArray {
1616

src/sg/nphau/leetcode/easy/RemoveDuplicates.java renamed to src/com/nphausg/leetcode/easy/RemoveDuplicates.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* https://leetcode.com/problems/remove-duplicates-from-sorted-array/
77
* </p>
88
*/
9-
package sg.nphau.leetcode.easy;
9+
package com.nphausg.leetcode.easy;
1010

1111
public class RemoveDuplicates {
1212
public int removeDuplicates(int[] nums) {

src/sg/nphau/leetcode/easy/RemoveElement.java renamed to src/com/nphausg/leetcode/easy/RemoveElement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* Copyright (c) 2021 . All rights reserved.
44
* Last modified 09/12/2021, 15:54
55
*/
6-
package sg.nphau.leetcode.easy;
6+
package com.nphausg.leetcode.easy;
77

88
/**
9-
* https://leetcode.com/problems/remove-element/
9+
* <a href="https://leetcode.com/problems/remove-element/">...</a>
1010
*/
1111
public class RemoveElement {
1212

src/sg/nphau/leetcode/easy/ValidParentheses.java renamed to src/com/nphausg/leetcode/easy/ValidParentheses.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
* Copyright (c) 2021 . All rights reserved.
44
* Last modified 09/12/2021, 15:54
55
*/
6-
package sg.nphau.leetcode.easy;
6+
package com.nphausg.leetcode.easy;
77

88
import java.util.Stack;
99

1010
/**
11-
* https://leetcode.com/problems/valid-parentheses
11+
* <a href="https://leetcode.com/problems/valid-parentheses">...</a>
1212
*/
1313
public class ValidParentheses {
1414
public boolean isValid(String s) {

src/sg/nphau/leetcode/hard/FirstMissingPositive.java renamed to src/com/nphausg/leetcode/hard/FirstMissingPositive.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2021 . All rights reserved.
44
* Last modified 09/12/2021, 15:54
55
*/
6-
package sg.nphau.leetcode.hard;
6+
package com.nphausg.leetcode.hard;
77

88
import java.util.Arrays;
99

src/sg/nphau/leetcode/medium/DecodeWays.java renamed to src/com/nphausg/leetcode/medium/DecodeWays.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* Last modified 09/12/2021, 15:54
55
*/
66

7-
package sg.nphau.leetcode.medium;
7+
package com.nphausg.leetcode.medium;
88

99
/**
10-
* https://leetcode.com/problems/decode-ways/
10+
* 91. Decode Ways
11+
* <a href="https://leetcode.com/problems/decode-ways/">...</a>
1112
*/
1213
public class DecodeWays {
1314

src/sg/nphau/leetcode/medium/FindClosestElements.java renamed to src/com/nphausg/leetcode/medium/FindClosestElements.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
* Copyright (c) 2021 . All rights reserved.
44
* Last modified 09/12/2021, 15:54
55
*/
6-
package sg.nphau.leetcode.medium;
6+
package com.nphausg.leetcode.medium;
77

88
import java.util.ArrayList;
99
import java.util.List;
1010

1111
/**
12-
* https://leetcode.com/problems/find-k-closest-elements/
12+
* 658. Find K Closest Elements
13+
* <a href="https://leetcode.com/problems/find-k-closest-elements/">...</a>
1314
*/
1415
public class FindClosestElements {
15-
public List<Integer> findClosestElements(int[] arr, int k, int x) {
16+
public static List<Integer> findClosestElements(int[] arr, int k, int x) {
1617
int left = 0, right = arr.length - k;
1718
while (left < right) {
1819
int mid = left + (right - left) / 2;

src/sg/nphau/leetcode/medium/LetterCombinations.java renamed to src/com/nphausg/leetcode/medium/LetterCombinations.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
* Copyright (c) 2021 . All rights reserved.
44
* Last modified 09/12/2021, 15:54
55
*/
6-
package sg.nphau.leetcode.medium;
6+
package com.nphausg.leetcode.medium;
77

88
import java.util.ArrayList;
99
import java.util.Collections;
1010
import java.util.List;
1111

1212
/**
13-
* https://leetcode.com/problems/letter-combinations-of-a-phone-number
13+
* 17. Letter Combinations of a Phone Number
14+
* <a href="https://leetcode.com/problems/letter-combinations-of-a-phone-number">...</a>
1415
*/
1516
public class LetterCombinations {
1617
public List<String> letterCombinations(String digits) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.nphausg.leetcode.medium;
2+
3+
/**
4+
* 1143. Longest Common Subsequence
5+
* The longest common subsequence problem is a classic dynamic programming problem.
6+
* The approach involves breaking down the problem into smaller subproblems and building up the solution using a DP table.
7+
* This method ensures that we efficiently compute the length of the longest common subsequence in
8+
* 𝑂(𝑚×𝑛) time.
9+
* <a href="https://leetcode.com/problems/longest-common-subsequence">...</a>
10+
*/
11+
public class LongestCommonSubsequence {
12+
13+
public static int longestCommonSubsequence(String text1, String text2) {
14+
if (text1.isEmpty() || text2.isEmpty()) {
15+
return 0;
16+
}
17+
18+
int m = text1.length();
19+
int n = text2.length();
20+
21+
// Create a 2D array to store the lengths of LCS
22+
int[][] dp = new int[m + 1][n + 1];
23+
24+
for (int i = 1; i <= m; i++) {
25+
for (int j = 1; j <= n; j++) {
26+
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
27+
dp[i][j] = dp[i - 1][j - 1] + 1;
28+
} else {
29+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
30+
}
31+
}
32+
}
33+
// The length of the longest common subsequence
34+
return dp[m][n];
35+
}
36+
}

src/sg/nphau/leetcode/Main.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)