Skip to content

fix longestCommonSubstring() to handle unicode characters (#129) #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ describe('longestCommonSubstring', () => {
expect(longestCommonSubstring('', 'ABC')).toBe('');
expect(longestCommonSubstring('ABABC', 'BABCA')).toBe('BABC');
expect(longestCommonSubstring('BABCA', 'ABCBA')).toBe('ABC');
expect(longestCommonSubstring('𐌵𐌵**ABC', '𐌵𐌵--ABC')).toBe('ABC');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
* @return {string}
*/
export default function longestCommonSubstring(s1, s2) {
// transform s1 & s2 into arrays to allow handling unicodes as single caracter.
const [a1, a2] = [s1, s2].map(s => Array.from(s));

// Init the matrix of all substring lengths to use Dynamic Programming approach.
const substringMatrix = Array(s2.length + 1).fill(null).map(() => {
return Array(s1.length + 1).fill(null);
const substringMatrix = Array(a2.length + 1).fill(null).map(() => {
return Array(a1.length + 1).fill(null);
});

// Fill the first row and first column with zeros to provide initial values.
for (let columnIndex = 0; columnIndex <= s1.length; columnIndex += 1) {
for (let columnIndex = 0; columnIndex <= a1.length; columnIndex += 1) {
substringMatrix[0][columnIndex] = 0;
}

for (let rowIndex = 0; rowIndex <= s2.length; rowIndex += 1) {
for (let rowIndex = 0; rowIndex <= a2.length; rowIndex += 1) {
substringMatrix[rowIndex][0] = 0;
}

Expand All @@ -23,9 +26,9 @@ export default function longestCommonSubstring(s1, s2) {
let longestSubstringColumn = 0;
let longestSubstringRow = 0;

for (let rowIndex = 1; rowIndex <= s2.length; rowIndex += 1) {
for (let columnIndex = 1; columnIndex <= s1.length; columnIndex += 1) {
if (s1[columnIndex - 1] === s2[rowIndex - 1]) {
for (let rowIndex = 1; rowIndex <= a2.length; rowIndex += 1) {
for (let columnIndex = 1; columnIndex <= a1.length; columnIndex += 1) {
if (a1[columnIndex - 1] === a2[rowIndex - 1]) {
substringMatrix[rowIndex][columnIndex] = substringMatrix[rowIndex - 1][columnIndex - 1] + 1;
} else {
substringMatrix[rowIndex][columnIndex] = 0;
Expand All @@ -50,7 +53,7 @@ export default function longestCommonSubstring(s1, s2) {
let longestSubstring = '';

while (substringMatrix[longestSubstringRow][longestSubstringColumn] > 0) {
longestSubstring = s1[longestSubstringColumn - 1] + longestSubstring;
longestSubstring = a1[longestSubstringColumn - 1] + longestSubstring;
longestSubstringRow -= 1;
longestSubstringColumn -= 1;
}
Expand Down