Skip to content

Commit fa051ac

Browse files
committedApr 24, 2025
Add solution #1638
1 parent f17ec24 commit fa051ac

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,439 LeetCode solutions in JavaScript
1+
# 1,440 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1262,6 +1262,7 @@
12621262
1632|[Rank Transform of a Matrix](./solutions/1632-rank-transform-of-a-matrix.js)|Hard|
12631263
1636|[Sort Array by Increasing Frequency](./solutions/1636-sort-array-by-increasing-frequency.js)|Easy|
12641264
1637|[Widest Vertical Area Between Two Points Containing No Points](./solutions/1637-widest-vertical-area-between-two-points-containing-no-points.js)|Easy|
1265+
1638|[Count Substrings That Differ by One Character](./solutions/1638-count-substrings-that-differ-by-one-character.js)|Medium|
12651266
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12661267
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12671268
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 1638. Count Substrings That Differ by One Character
3+
* https://leetcode.com/problems/count-substrings-that-differ-by-one-character/
4+
* Difficulty: Medium
5+
*
6+
* Given two strings s and t, find the number of ways you can choose a non-empty substring of s
7+
* and replace a single character by a different character such that the resulting substring is
8+
* a substring of t. In other words, find the number of substrings in s that differ from some
9+
* substring in t by exactly one character.
10+
*
11+
* For example, the underlined substrings in "computer" and "computation" only differ by the
12+
* 'e'/'a', so this is a valid way.
13+
*
14+
* Return the number of substrings that satisfy the condition above.
15+
*
16+
* A substring is a contiguous sequence of characters within a string.
17+
*/
18+
19+
/**
20+
* @param {string} s
21+
* @param {string} t
22+
* @return {number}
23+
*/
24+
var countSubstrings = function(s, t) {
25+
let result = 0;
26+
27+
for (let i = 0; i < s.length; i++) {
28+
for (let j = 0; j < t.length; j++) {
29+
let diffCount = 0;
30+
for (let k = 0; i + k < s.length && j + k < t.length; k++) {
31+
if (s[i + k] !== t[j + k]) diffCount++;
32+
if (diffCount === 1) result++;
33+
if (diffCount > 1) break;
34+
}
35+
}
36+
}
37+
38+
return result;
39+
};

0 commit comments

Comments
 (0)
Please sign in to comment.