Inputs are given in below format:-
CHAR->CHAR->CHAR->......any number times = INTEGER
Here, CHAR - any character which represents Key. INTEGER - any integer value which represents value of this relation.
There are number of such sequences given to us. We have to produce JSON format for that.
Example :-
a->b = 12
a->b = 20
a->c->d = 190
a->d = 300
a->c->e = 90
c = 18
Output :-
{
a : {
b :[12, 20],
c : {
d : [190]
e : [90]
}
d : [300]
}
c : [18]
}
Wrong Case
If any key has value and it going to point to any other key, then it should not possible
Example :-
a : {
[15],
d : {
// something here
}
}
My algorithm :-
- I used Linked List data structure for building this pattern.
- Use two arrays, Nodes, and Forest, every index represents one character, means 0 represents a, .... 25 represents z.
- Nodes contains all the linking from one key to other, an if it end, then it contains array of integer that shows values.
- Forest represents all the roots of different trees, for above case,
aandcare roots for two trees. - Perform all the linkings and update Nodes, and Forest both array.
- Finally at the end, iterate through Forest array and if it true create a tree using this as a root.
This is my algorithm, but it is not correct. Please help me in correcting my algorithm or developing new one.
ForestandNodedata structures and wrote the algo at least in psedo-code.