BackmediumStrings

Categorical Array Segmentation Solution

Problem Statement

Write a function to categorize an array of strings into clusters, such that two strings belong to the same cluster if and only if they contain the same characters, regardless of order or frequency.

Example 1
Input
["eat","tea","tan","ate","nat","bat"]
Output
[["eat","tea","ate"],["tan","nat"],["bat"]]

Explanation: The strings 'eat', 'tea', and 'ate' are anagrams of each other, as are 'tan' and 'nat'. The string 'bat' does not share a cluster with any other string.

Example 2
Input
["cat","dog","god","tac","good"]
Output
[["cat","tac"],["dog","god"],["good"]]

Explanation: The strings 'cat' and 'tac' are anagrams, as are 'dog' and 'god'. The string 'good' does not share a cluster with any other string.

Example 3
Input
["listen","silent","enlist","inlets","tinsel"]
Output
[["listen","silent","enlist","inlets","tinsel"]]

Explanation: All strings in the input array are anagrams of each other.

Constraints

  • 1 <= array length <= 100
  • 1 <= string length <= 100
  • All strings contain only lowercase English letters.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free