@@ -184,7 +184,7 @@ class C {
184
184
185
185
//================
186
186
187
- class Node {
187
+ class ANode {
188
188
late String c;
189
189
late int key;
190
190
late List <Node > cs;
@@ -304,50 +304,108 @@ class E {
304
304
}
305
305
}
306
306
307
- class TreeNode {
308
- late String val;
309
- late int index;
310
- late List <TreeNode > child;
311
- TreeNode (String val, int index) {
312
- this .val = val;
313
- this .index = index;
314
- child = [];
307
+ class Node {
308
+ late List <Node > next;
309
+ late int id;
310
+
311
+ Node (int id) {
312
+ next = [];
313
+ this .id = id;
315
314
}
316
315
}
317
316
318
317
class F {
319
- late List <int > res;
320
- late List <bool > visited;
321
318
List <int > countSubTrees (int n, List <List <int >> edges, String labels) {
322
- List <TreeNode > nodes = List .filled (n, TreeNode (labels, 0 ));
323
- // nodes.add(n);
324
- for (int i = 0 ; i < n; ++ i) nodes[i] = new TreeNode (labels[i], i);
325
- for (int i = 0 ; i < n - 1 ; ++ i) {
326
- int a = edges[i][0 ], b = edges[i][1 ];
327
- nodes[a].child.add (nodes[b]);
328
- nodes[b].child.add (nodes[a]);
319
+ List <int > res = List .filled (n, 0 , growable: true );
320
+ HashMap <int , Node > map = HashMap ();
321
+ Node root = new Node (0 );
322
+ map[0 ] = root;
323
+ for (List <int > e in edges) {
324
+ if (! map.containsKey (e[0 ])) {
325
+ Node t = new Node (e[0 ]);
326
+ map[e[0 ]] = t;
327
+ map[e[1 ]]? .next.add (t);
328
+ } else {
329
+ Node t = new Node (e[1 ]);
330
+ map[e[1 ]] = t;
331
+ map[e[0 ]]? .next.add (t);
332
+ }
329
333
}
330
- TreeNode root = nodes[0 ];
331
- res = List .filled (n, 0 );
332
- visited = List .filled (n, false );
333
- List .filled (res.length, 1 );
334
- dfs (root);
334
+ List <String > arr = labels.split ("" );
335
+ dfs (root, arr, res);
335
336
return res;
336
337
}
337
338
338
- Map <String , int > dfs (TreeNode root) {
339
- HashMap <String , int > map = HashMap ();
340
- if (root == null ) return map;
341
- visited[root.index] = true ;
342
- map[root.val] = (map[root.val] ?? 0 ) + 1 ;
343
- for (TreeNode child in root.child) {
344
- if (visited[child.index]) continue ;
345
- Map <String , int > temp = dfs (child);
346
- if (temp.containsKey (root.val)) res[root.index] += temp[root.val]! ;
347
- for (String key in temp.keys) {
348
- map[key] = (map[key] ?? 0 ) + temp[key]! ;
339
+ List <int > dfs (Node ? node, List <String > arr, List <int > res) {
340
+ if (node == null ) {
341
+ return [];
342
+ }
343
+ if (node.next.length == 0 ) {
344
+ List <int > record = List .filled (26 , 0 );
345
+ record[arr[node.id].codeUnitAt (0 ) - 'a' .codeUnitAt (0 )]++ ;
346
+ res[node.id] = record[arr[node.id].codeUnitAt (0 ) - 'a' .codeUnitAt (0 )];
347
+ return record;
348
+ }
349
+ List <int > r = List .filled (26 , 0 );
350
+ for (Node n in node.next) {
351
+ List <int > t = dfs (n, arr, res);
352
+ for (int i = 0 ; i < 26 ; i++ ) {
353
+ r[i] += t[i];
349
354
}
350
355
}
351
- return map;
356
+ r[arr[node.id].codeUnitAt (0 ) - 'a' .codeUnitAt (0 )]++ ;
357
+ res[node.id] = r[arr[node.id].codeUnitAt (0 ) - 'a' .codeUnitAt (0 )];
358
+ return r;
352
359
}
353
360
}
361
+ /*
362
+
363
+
364
+ class Solution {
365
+ public int[] countSubTrees(int n, int[][] edges, String labels) {
366
+ int[] res = new int[n];
367
+ Map<Integer, Node> map = new HashMap<>();
368
+ Node root = new Node(0);
369
+ map.put(0, root);
370
+ for (int[] e : edges) {
371
+ if (!map.containsKey(e[0])) {
372
+ Node t = new Node(e[0]);
373
+ map.put(e[0], t);
374
+ map.get(e[1]).nexts.add(t);
375
+ } else {
376
+ Node t = new Node(e[1]);
377
+ map.put(e[1], t);
378
+ map.get(e[0]).nexts.add(t);
379
+ }
380
+ }
381
+ char[] arr = labels.toCharArray();
382
+ dfs(root, arr, res);
383
+ return res;
384
+ }
385
+
386
+ int[] dfs(Node node, char[] arr, int[] res) {
387
+ if (node == null) {
388
+ return null;
389
+ }
390
+ if (node.nexts.size() == 0) {
391
+ int[] record = new int[26];
392
+ record[arr[node.id] - 'a']++;
393
+ res[node.id] = record[arr[node.id] - 'a'];
394
+ return record;
395
+ }
396
+ int[] r = new int[26];
397
+ for (Node n : node.nexts) {
398
+ int[] t = dfs(n, arr, res);
399
+ for (int i = 0; i < 26; i++) {
400
+ r[i] += t[i];
401
+ }
402
+ }
403
+ r[arr[node.id] - 'a']++;
404
+ res[node.id] = r[arr[node.id] - 'a'];
405
+ return r;
406
+ }
407
+
408
+
409
+ }
410
+
411
+ */
0 commit comments