-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1638-count-substrings-that-differ-by-one-character.js
More file actions
39 lines (36 loc) · 1.21 KB
/
1638-count-substrings-that-differ-by-one-character.js
File metadata and controls
39 lines (36 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 1638. Count Substrings That Differ by One Character
* https://leetcode.com/problems/count-substrings-that-differ-by-one-character/
* Difficulty: Medium
*
* Given two strings s and t, find the number of ways you can choose a non-empty substring of s
* and replace a single character by a different character such that the resulting substring is
* a substring of t. In other words, find the number of substrings in s that differ from some
* substring in t by exactly one character.
*
* For example, the underlined substrings in "computer" and "computation" only differ by the
* 'e'/'a', so this is a valid way.
*
* Return the number of substrings that satisfy the condition above.
*
* A substring is a contiguous sequence of characters within a string.
*/
/**
* @param {string} s
* @param {string} t
* @return {number}
*/
var countSubstrings = function(s, t) {
let result = 0;
for (let i = 0; i < s.length; i++) {
for (let j = 0; j < t.length; j++) {
let diffCount = 0;
for (let k = 0; i + k < s.length && j + k < t.length; k++) {
if (s[i + k] !== t[j + k]) diffCount++;
if (diffCount === 1) result++;
if (diffCount > 1) break;
}
}
}
return result;
};