Problem · String

Similar Text Substring Count

EasyAmazonNEW GRADOA
See Amazon hiring insights

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.

Examples
01 · Example 1
target = "moon"
content = "monomon"
return = 2
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.
02 · Example 2
target = "aaa"
content = "aaaa"
return = 2
There are 2 substrings of "aaaa" that are similar to "aaa" are: aaaa aaaa
Constraints
  • target and content will consist solely of lowercase English letters.
  • 1 ≤ |target| ≤ |content| ≤ 50, where |s| denotes the length of a string s.
More Amazon problems
drafts saved locally
public int similarTextSubstringCount(String target, String content) {
  // write your code here
}
target"moon"
content"monomon"
expected2
sign in to submit