#C14896. Anagram Finder in Nested Structures
Anagram Finder in Nested Structures
Anagram Finder in Nested Structures
Given a word and a candidate list that may contain nested arrays, your task is to identify all distinct anagrams of the given word. Only string elements are considered. A candidate is an anagram if the sorted sequence of its letters (after removing all non-alphabetic characters and converting to lowercase) equals that of the given word.
If a candidate is a nested array, process it recursively and preserve the nested structure in the output. Non-string elements are skipped.
Note: For a string s, let \(f(s)=\text{sorted}(\text{sanitize}(s))\), where \(\text{sanitize}(s)\) means removing all non-alphabetic characters and converting to lowercase. Two strings are anagrams if \(f(s_1)=f(s_2)\).
inputFormat
The input is given on two lines from standard input:
- The first line is a non-empty string representing the word.
- The second line is a JSON array representing the candidate list. The array may include nested arrays and non-string elements (which should be ignored).
outputFormat
Print to standard output a JSON array representing the list of anagrams found. The structure of nested arrays must be preserved as in the input. If no anagrams are found, output an empty JSON array []
.
listen
["enlists", "google", "inlets", "banana", ["Silent", "LISTEN"], "tinsel", [null, "Listen"]]
["inlets",["silent","listen"],"tinsel",["listen"]]