Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef29103

Browse files
committed
05.01 (1) substring problem
1 parent1e77bc8 commitef29103

File tree

4 files changed

+193
-1
lines changed

4 files changed

+193
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Time : O(n * m) ; Space: O()
3+
* @tag : LintCode Copyright; Dynamic Programming
4+
* @by : Steven Cooks
5+
* @date: Sep 6, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* Given two strings, find the longest common substring.
10+
* Return the length of it.
11+
*
12+
* Example Given A = "ABCD", B = "CBCE", return 2.
13+
*
14+
* Note
15+
* The characters in substring should occur continuously in original string.
16+
* This is different with subsequence.
17+
*
18+
***************************************************************************
19+
* {@link http://www.lintcode.com/en/problem/longest-common-substring/ }
20+
*/
21+
package_01_LongestCommonSubstring;
22+
23+
/** see test {@link _01_LongestCommonSubstring.SolutionTest } */
24+
publicclassSolution {
25+
26+
publicintlongestCommonSubstring(StringA,StringB) {
27+
intlen1 =A.length();
28+
intlen2 =B.length();
29+
intres =0;
30+
int[][]dp =newint[len1 +1][len2 +1];
31+
// take advantage of default value of array
32+
for (inti =len1 -1;i >=0;i--) {
33+
for (intj =len2 -1;j >=0;j--) {
34+
intcommon =0;
35+
if (i <len1 &&j <len2 &&A.charAt(i) ==B.charAt(j)) {
36+
common =1 +dp[i +1][j +1];
37+
}
38+
dp[i][j] =common;
39+
res =Math.max(res,common);
40+
}
41+
}
42+
returnres;
43+
}
44+
45+
publicintlongestCommonSubstringTemplate(StringA,StringB) {
46+
intlen1 =A.length();
47+
intlen2 =B.length();
48+
intres =0;
49+
int[][]dp =newint[len1 +1][len2 +1];
50+
for (inti =len1;i >=0;i--) {
51+
for (intj =len2;j >=0;j--) {
52+
intcommon =0;
53+
if (i ==len1 &&j ==len2) {
54+
common =0;
55+
}elseif (i ==len1) {
56+
common =0;
57+
}elseif (j ==len2) {
58+
common =0;
59+
}else {
60+
if (A.charAt(i) ==B.charAt(j)) {
61+
common =1 +dp[i +1][j +1];
62+
}else {
63+
common =0;
64+
}
65+
}
66+
dp[i][j] =common;
67+
res =Math.max(res,common);
68+
}
69+
}
70+
returnres;
71+
}
72+
73+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Time : O(n * m) ; Space: O()
3+
* @tag : LintCode Copyright; Dynamic Programming
4+
* @by : Steven Cooks
5+
* @date: Sep 6, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* Given two strings, find the longest common substring.
10+
* Return the length of it.
11+
*
12+
* Example Given A = "ABCD", B = "CBCE", return 2.
13+
*
14+
* Note
15+
* The characters in substring should occur continuously in original string.
16+
* This is different with subsequence.
17+
*
18+
***************************************************************************
19+
* {@link http://www.lintcode.com/en/problem/longest-common-substring/ }
20+
*/
21+
package_01_LongestCommonSubstring;
22+
23+
/** see test {@link _01_LongestCommonSubstring.SolutionTest } */
24+
publicclassSolution2 {
25+
26+
publicintlongestCommonSubstring(StringA,StringB) {
27+
intlen1 =A.length();
28+
intlen2 =B.length();
29+
intres =0;
30+
for (inti =0;i <len1;i++) {
31+
for (intj =0;j <len2;j++) {
32+
intp =i;
33+
intq =j;
34+
while (p <len1 &&q <len2 &&A.charAt(p) ==B.charAt(q)) {
35+
p++;
36+
q++;
37+
}
38+
res =Math.max(res,p -i);
39+
}
40+
}
41+
returnres;
42+
}
43+
44+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package_01_LongestCommonSubstring;
2+
3+
importstaticorg.junit.Assert.*;
4+
5+
importorg.junit.After;
6+
importorg.junit.Before;
7+
importorg.junit.Rule;
8+
importorg.junit.Test;
9+
importorg.junit.rules.Timeout;
10+
11+
publicclassSolutionTest {
12+
13+
/** Test method for {@link _01_LongestCommonSubstring.Solution } */
14+
Solutionsolution;
15+
16+
@Rule
17+
publicTimeoutglobalTimeout =newTimeout(200);
18+
19+
@Before
20+
publicvoidsetUp()throwsException {
21+
solution =newSolution();
22+
}
23+
24+
@After
25+
publicvoidtearDown()throwsException {
26+
solution =null;
27+
}
28+
29+
@Test
30+
publicvoidTest1() {
31+
StringA ="ABCD";
32+
StringB ="CBCE";
33+
intactual =solution.longestCommonSubstring(A,B);
34+
intexpected =2;
35+
assertEquals(expected,actual);
36+
}
37+
38+
@Test
39+
publicvoidTest2() {
40+
StringA ="ABC";
41+
StringB ="DEF";
42+
intactual =solution.longestCommonSubstring(A,B);
43+
intexpected =0;
44+
assertEquals(expected,actual);
45+
}
46+
47+
@Test
48+
publicvoidTest3() {
49+
StringA ="ABC";
50+
StringB ="ABCDEFG";
51+
intactual =solution.longestCommonSubstring(A,B);
52+
intexpected =3;
53+
assertEquals(expected,actual);
54+
}
55+
56+
@Test
57+
publicvoidTest4() {
58+
StringA ="DEFG";
59+
StringB ="ABCDEFG";
60+
intactual =solution.longestCommonSubstring(A,B);
61+
intexpected =4;
62+
assertEquals(expected,actual);
63+
}
64+
65+
@Test
66+
publicvoidTest5() {
67+
StringA ="CDEFG";
68+
StringB ="ABCDEFGHI";
69+
intactual =solution.longestCommonSubstring(A,B);
70+
intexpected =5;
71+
assertEquals(expected,actual);
72+
}
73+
74+
}

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ Selected problems that are included in LintCode but are not included in Leetcode
1313
| ---| ----| ---------| -----|
1414
| 03.01|[Insert Node in Binary Search Tree](https://github.com/interviewcoder/lintcode/blob/master/03_binarytree%26divideconquer/_01_InsertNodeInABinarySearchTree/Solution.java)| BST`LintCode Copyright`||
1515
| 03.02|[Search Range In Binary Search Tree](https://github.com/interviewcoder/lintcode/blob/master/03_binarytree%26divideconquer/_02_SearchRangeInBinarySearchTree/Solution.java)| Binary Search Tree| in-order + pruning|
16-
| 04.01|[Longest Increasing Subsequence](https://github.com/interviewcoder/lintcode/blob/master/04_dynamicprogrammingI/_01_LongestIncreasingSubsequence/Solution.java)| Dynamic Programming||
16+
| 04.01|[Longest Increasing Subsequence](https://github.com/interviewcoder/lintcode/blob/master/04_dynamicprogrammingI/_01_LongestIncreasingSubsequence/SolutConciseion.java)| Dynamic Programming||
17+
| 05.01|[Longest Common Substring](https://github.com/interviewcoder/lintcode/blob/master/05_dynamicprogrammingII/_01_LongestCommonSubstring/SolutConciseion.java)| Dynamic Programming||

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp