You are given a multiset of lowercase letters (as a string s, possibly with repeated letters) and two distinct lowercase letters x and y that are known to "clash". Count the number of DISTINCT arrangements (as strings) of all the letters in s — treating repeated letters as indistinguishable, so arrangements that look identical as strings are counted once — such that x and y never appear directly next to each other anywhere in the arrangement.
If x or y does not occur in s at all, the adjacency condition can never be violated, so every distinct arrangement of s counts.
Line 1: the string s.
Line 2: two distinct lowercase letters x and y, space-separated.
A single integer: the number of distinct arrangements of s in which x and y are never adjacent.
Example 1
Input
aabc b c
Expected
6
Explanation
There are 12 distinct arrangements of the letters a,a,b,c. Gluing 'b' and 'c' together (in either order) and arranging that block with the two identical a's gives 3!/2!=3 arrangements times 2 orders = 6 where they are adjacent, so 12-6=6 arrangements keep 'b' and 'c' apart.
Example 2
Input
ab a b
Expected
0
Explanation
With only two letters 'a' and 'b', any arrangement places them adjacent to each other, so 0 arrangements avoid the clash.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →