Skip to content

Commit c89f594

Browse files
committed
added isOneEditDistanceDBG()
1 parent 6f2cd7b commit c89f594

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

src/main.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { searchDBG } from "./array/search-in-rotated-sorted-array";
22
import { flatDBG } from "./js_core/flatten";
33
import { findAnagramsDBG } from "./string/find-anagrams";
4+
import { lengthOfLongestSubstringDBG } from "./string/longest-substring.string";
45
import { testValidPalindrome } from "./string/valid-palindrome";
6+
import { EventEmitter } from 'node:events';
57

6-
testValidPalindrome()
8+
9+
isOneEditDistanceDBG();

src/string/is-one-edit-distance.ts

+28
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,31 @@ function isOneEditDistance(s: string, t: string): boolean {
1919
}
2020
return lenS + 1 === lenT;
2121
}
22+
23+
export const isOneEditDistanceDBG = () => {
24+
const tests = [
25+
{
26+
input: { s: "ab", t: "acb" },
27+
result: true
28+
},
29+
{
30+
input: { s: "cab", t: "ad" },
31+
result: false
32+
},
33+
{
34+
input: { s: "1203", t: "1213" },
35+
result: true
36+
}
37+
];
38+
39+
tests.forEach((test, index) => {
40+
const result = isOneEditDistance(test.input.s, test.input.t);
41+
if (result === test.result) {
42+
console.log(`${index} success`);
43+
} else {
44+
console.log(`${index} fail`);
45+
console.log(`expected ${test.result}`);
46+
console.log(`got ${result}`);
47+
}
48+
});
49+
}
+40-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,49 @@
11
export function lengthOfLongestSubstring(s: string): number {
22
let maxLength = 0;
3-
let right = 0;
3+
let left = 0;
44
const charIndexMap = new Map<string, number>();
55

6-
for (let left = 0; left < s.length; left++) {
7-
const char = s[left];
6+
for (let right = 0; right < s.length; right++) {
7+
const char = s[right];
88

9-
if (charIndexMap.has(char) && charIndexMap.get(char) >= right) {
10-
right = charIndexMap.get(char)! + 1;
9+
if (charIndexMap.has(char) && charIndexMap.get(char) >= left) {
10+
left = charIndexMap.get(char)! + 1;
1111
}
12-
charIndexMap.set(char, left);
13-
maxLength = Math.max(maxLength, left - right + 1);
12+
charIndexMap.set(char, right);
13+
maxLength = Math.max(maxLength, right - left + 1);
1414
}
1515
return maxLength;
16+
}
17+
18+
19+
export const lengthOfLongestSubstringDBG = () => {
20+
const tests = [
21+
{
22+
input: "abcabcdbb",
23+
result: 4, // "abc"
24+
},
25+
{
26+
input: "bbbbb",
27+
result: 1, // "b"
28+
},
29+
{
30+
input: "pwwkew",
31+
result: 3, // "wke"
32+
},
33+
{
34+
input: "",
35+
result: 0, // пустая строка
36+
}
37+
];
38+
39+
tests.forEach((test, index) => {
40+
const result = lengthOfLongestSubstring(test.input);
41+
if (result === test.result) {
42+
console.log(`${index} success`);
43+
} else {
44+
console.log(`${index} fail`);
45+
console.log(`expected ${test.result}`);
46+
console.log(`got ${result}`);
47+
}
48+
});
1649
}

0 commit comments

Comments
 (0)