Fruit Connectivity
You are given a string representing different types of fruits, where each fruit is represented by its initial (e.g., 'A' for Apple, 'B' for Banana). Your task is to create a graph where each node represents a fruit, and an edge between two nodes exists if the corresponding fruits appear consecutively in the string at least once. Return a representation of the graph showing all nodes and their respective connections.
[ "ABAB" ]
Explanation. The input string 'ABAB' shows 'A' and 'B' appearing consecutively. A graph representation would have edges connecting 'A' to 'B' and 'B' to 'A'.
[ "A" ]
Explanation. The input string 'A' contains only one type of fruit resulting in a graph with node 'A' having no edges.
[ "ABCAC" ]
Explanation. Here, 'A' connects to both 'B' and 'C', 'B' connects to 'A' and 'C', and 'C' connects to 'B' and 'A'.
Follow-up: Can you modify your solution to handle inputs where fruit types are represented by more than one letter?
The input string will only contain uppercase letters and will be at least one character long.
- Views
- 3