FastPrepFastPrep
Problem Brief

Similar Text Substring Count

NEW GRADOA

People browsing Amazon frequently rely on product feedback to make buying decisions. They may narrow down their search using specific phrases, but spelling errors often appear in reviews. To handle this issue, Amazon's system also considers review sections that contain words nearly matching the search phrase.

A text fragment phrase is nearly identical to another string target if you can swap two neighboring characters in phrase at most once to make it identical to target.

Your task is to determine the number of continuous sections within content that are nearly identical to target.

Note: A continuous section means consecutive characters inside a string. Two sections are distinct if they start from different indices.

1Example 1

Input
target = "moon", content = "monomon"
Output
2
Explanation
Take the first four letters from content, which is "mono". By swapping the last two letters, it becomes identical to the target "moon". Next, observe the final four letters in content, which is "onom". Swapping the first two characters transforms it into the target. Therefore, there are 2 continuous sections in "monom" that are nearly identical to "moon". Note that no other segment within the content qualifies as similar to the given target.

2Example 2

Input
target = "aaa", content = "aaaa"
Output
2
Explanation
There are 2 substrings of "aaaa" that are similar to "aaa" are: aaaa aaaa

Constraints

Limits and guarantees your solution can rely on.

  • target and content will consist solely of lowercase English letters.
  • 1 ≤ |target| ≤ |content| ≤ 50, where |s| denotes the length of a string s.
public int similarTextSubstringCount(String target, String content) {
  // write your code here
}
Input

target

"moon"

content

"monomon"

Output

2

Sign in to submit your solution.