Generate Anagrammed IDs — Problem Statement & Solution Guide
Problem Description
Given a list of strings representing tool types, a list of strings representing returned statuses, and a list of integers representing collection IDs, generate a list of unique identifiers by anagramming the concatenation of each tool type, returned status, and collection ID.
Examples
Input
tool_types = ['abc', 'def'], statuses = ['123', '456'], collection_ids = [1, 2]
Output
['abc123', 'bac123', 'cab123', 'def456', 'fed456', 'dfe456']
Explanation: Step 1: Concatenate each tool type, status, and collection ID to form a string. For example, 'abc123' is formed by concatenating 'abc', '123', and 1. Step 2: Generate all permutations of the concatenated string. For 'abc123', the permutations are ['abc123', 'bac123', 'cab123']. Step 3: Collect all unique permutations from each string.
Input
tool_types = ['abc', 'def'], statuses = ['123', '456'], collection_ids = [1, 1]
Output
['abc123', 'bac123', 'cab123', 'abc123', 'bac123', 'cab123']
Explanation: Step 1: Concatenate each tool type, status, and collection ID to form a string. For example, 'abc123' is formed by concatenating 'abc', '123', and 1. Step 2: Generate all permutations of the concatenated string. For 'abc123', the permutations are ['abc123', 'bac123', 'cab123']. Step 3: Collect all unique permutations from each string.
Constraints
- 1 ≤ N ≤ 10^5
- Each tool name has a maximum length of 10 characters
- Each status (success/error) is exactly 8 characters long (including the spaces)
- Each collection ID is a positive integer with up to 6 digits
Optimal Approach & Strategy
A more efficient approach would be to store the tool names and their corresponding IDs in a hash map. Then, use the map to generate the anagrammed IDs based on the tool name and returned status, resulting in a time complexity of O(n).
Brute Force Approach
A direct approach to solving this problem would be to generate permutations of each tool name with its corresponding status and ID. However, this approach is inefficient with a time complexity of O(n^6) due to the large number of permutations generated.
Verified Code Solutions
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Solution {
public List<String> generateAnagrammedIds(String[] toolTypes, String[] returnedStatuses, int[] collectionIds) {
// Generate all permutations of each concatenated string
List<String> anagrammedIds = IntStream.range(0, toolTypes.length)
.mapToObj(i -> IntStream.range(0, returnedStatuses.length)
.mapToObj(j -> IntStream.range(0, collectionIds.length)
.mapToObj(k -> String.join("", toolTypes[i], returnedStatuses[j], String.valueOf(collectionIds[k])))
.boxed()
.flatMap(p -> p.stream().map(s -> String.valueOf(s.toCharArray())))
.boxed()
.collect(Collectors.toList()))
.flatMap(p -> p.stream().map(s -> String.valueOf(s.toCharArray())))
.boxed()
.collect(Collectors.toList()))
.flatMap(p -> p.stream().map(s -> String.valueOf(s.toCharArray())))
.boxed()
.collect(Collectors.toList());
return anagrammedIds;
}
}import itertools
def generate_anagrammed_ids(tool_types, returned_statuses, collection_ids):
# Generate all permutations of each concatenated string
anagrammed_ids = [''.join(p) for p in itertools.permutations(''.join(t + s + str(i)) for t, s, i in zip(tool_types, returned_statuses, collection_ids))]
return anagrammed_ids
Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.