Problem · Array

String-Pair Frequency Similarity

Learn this problem
EasyIBM logoIBMFULLTIMEOA
See IBM hiring insights

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

drafts saved locally
public String[] areSimilar(String[] s, String[] t) {
    // write your code here
}
s["aabaab","aaaaabb"]
t["bbabbc","abbbbbb"]
expected["YES", "NO"]
checking account