FastPrepString-Pair Frequency Similarity
Problems/String-Pair Frequency Similarity

String-Pair Frequency Similarity

IBM logoIBM● EasyFULLTIMEOA
QuestionFunctionExamplesConstraints

Problem statement

You are given two arrays of strings, s and t, each of length n. Each pair (s[i], t[i]) contains two lowercase English strings.

Two strings are considered similar when:

  • Every letter x from 'a' through 'z' is considered.
  • The absolute difference between the number of times x appears in the two strings is at most 3.

For each pair (s[i], t[i]), check whether the strings are similar. Return an array of n elements where each element is:

  • "YES" if the pair is similar.
  • "NO" otherwise.

Function

areSimilar(s: String[], t: String[]) → String[]

Examples

Example 1

s = ["aabaab","aaaaabb"]t = ["bbabbc","abbbbbb"]return = ["YES","NO"]
Analysis of s[0] and t[0]
Letters[0] countt[0] countDifference
a413
b242
c011
Analysis of s[1] and t[1]
Letters[1] countt[1] countDifference
a514
b264

The numbers of occurrences of a, b, and c in s[0] and t[0] never differ by more than 3, so this pair is similar. In s[1] and t[1], both a and b violate the condition, so this pair is not similar.

Constraints

  • 1 <= s.length = t.length <= 5.
  • Each string contains only lowercase English letters.
  • Every string has length from 1 through 100000, inclusive.

More IBM problems

  • Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
  • Total Each Letter's Count in an Encoded StringOA · Seen Nov 2026
  • Count Servers With No Requests in a Time WindowOA · Seen Nov 2026
All IBM interview questionsSee IBM hiring insights
Saved
class Solution {
    public String[] areSimilar(String[] s, String[] t) {
        // write your code here
    }

}
Checking account…