Problem Β· String
Count Similar Substrings (Microsoft India) π±
Learn this problemProblem statement
A string s, is similar to another string t, if it possible to swap two adjacent characters at most once in s to turn it into t. Given a keyword string named key, find how many substrings of text are similar to key.
Function
countSimilarSubstrings(key: String, text: String) β intExamples
Example 1
key = "moon"text = "monomon"return = 2Consider the first four characters in text. i.e "mono". Swap the last two characters to match the keyword "moon".
The last four characters in the text are "omon". Swap the first two characters to match the keyword.
Thus, there are 2 substrings of "monomon" that are similar to "moon". Note, that no other substring is similar to the given key.
Example 2
key = "aaa"text = "aaaa"return = 2There are 2 substrings of "aaaa" that are similar to "aaa" are:
aaaa
aaaa
Example 3
key = "xxy"text = "zxxyxyx"return = 3No explanation is provided for now π©βπΎ
Constraints
key and text will consist solely of lowercase English letters.1 <= |key| <= |text| <= 50, where |s| denotes the length of a string s.