SUBANAGR - Editorial

PROBLEM LINKS

Practice
Contest

DIFFICULTY

EASY

EXPLANATION

It’s easy to check if a string is an anagram or a subsequence of some other string. Is it easy to check if a string is an anagram of some subsequence (a subanagram) of some other string? Yes, it is!

To see that, notice that if string A contains more letters ‘a’ than string B, then A can’t be a subanagram of B. The same is the case for any other letter. If no such letters exist, it’s obvious that A is a subanagram of B – we may remove extra letters from B to get A’s anagram.

When we’re dealing with a set of strings, it’s easy to see that each letter’s quantity in the resulting string can’t exceed the corresponding quantity in each string from the set. As we need the longest possible string, let’s set each letter’s quantity to be equal to the smallest of such quantities in the strings of the set. Lastly, as we need the lexicographically smallest string, output the resulting letters in alphabetical order as their order doesn’t matter.

SETTER’S SOLUTION

Can be found here.

TESTER’S SOLUTION

Can be found here.

4 Likes