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

Commit82ac89b

Browse files
daddou-matrekhleb
authored andcommitted
fix longestCommonSubstring() to handle unicode characters (trekhleb#129) (trekhleb#176)
1 parente09d526 commit82ac89b

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

‎src/algorithms/string/longest-common-substring/__test__/longestCommonSubstring.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ describe('longestCommonSubstring', () => {
77
expect(longestCommonSubstring('','ABC')).toBe('');
88
expect(longestCommonSubstring('ABABC','BABCA')).toBe('BABC');
99
expect(longestCommonSubstring('BABCA','ABCBA')).toBe('ABC');
10+
expect(longestCommonSubstring('𐌵𐌵**ABC','𐌵𐌵--ABC')).toBe('ABC');
1011
});
1112
});

‎src/algorithms/string/longest-common-substring/longestCommonSubstring.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
*@return {string}
55
*/
66
exportdefaultfunctionlongestCommonSubstring(s1,s2){
7+
// transform s1 & s2 into arrays to allow handling unicodes as single caracter.
8+
const[a1,a2]=[s1,s2].map(s=>Array.from(s));
9+
710
// Init the matrix of all substring lengths to use Dynamic Programming approach.
8-
constsubstringMatrix=Array(s2.length+1).fill(null).map(()=>{
9-
returnArray(s1.length+1).fill(null);
11+
constsubstringMatrix=Array(a2.length+1).fill(null).map(()=>{
12+
returnArray(a1.length+1).fill(null);
1013
});
1114

1215
// Fill the first row and first column with zeros to provide initial values.
13-
for(letcolumnIndex=0;columnIndex<=s1.length;columnIndex+=1){
16+
for(letcolumnIndex=0;columnIndex<=a1.length;columnIndex+=1){
1417
substringMatrix[0][columnIndex]=0;
1518
}
1619

17-
for(letrowIndex=0;rowIndex<=s2.length;rowIndex+=1){
20+
for(letrowIndex=0;rowIndex<=a2.length;rowIndex+=1){
1821
substringMatrix[rowIndex][0]=0;
1922
}
2023

@@ -23,9 +26,9 @@ export default function longestCommonSubstring(s1, s2) {
2326
letlongestSubstringColumn=0;
2427
letlongestSubstringRow=0;
2528

26-
for(letrowIndex=1;rowIndex<=s2.length;rowIndex+=1){
27-
for(letcolumnIndex=1;columnIndex<=s1.length;columnIndex+=1){
28-
if(s1[columnIndex-1]===s2[rowIndex-1]){
29+
for(letrowIndex=1;rowIndex<=a2.length;rowIndex+=1){
30+
for(letcolumnIndex=1;columnIndex<=a1.length;columnIndex+=1){
31+
if(a1[columnIndex-1]===a2[rowIndex-1]){
2932
substringMatrix[rowIndex][columnIndex]=substringMatrix[rowIndex-1][columnIndex-1]+1;
3033
}else{
3134
substringMatrix[rowIndex][columnIndex]=0;
@@ -50,7 +53,7 @@ export default function longestCommonSubstring(s1, s2) {
5053
letlongestSubstring='';
5154

5255
while(substringMatrix[longestSubstringRow][longestSubstringColumn]>0){
53-
longestSubstring=s1[longestSubstringColumn-1]+longestSubstring;
56+
longestSubstring=a1[longestSubstringColumn-1]+longestSubstring;
5457
longestSubstringRow-=1;
5558
longestSubstringColumn-=1;
5659
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp