Advertisement
Kali_prasad

A20250306a_DEshaw

Mar 7th, 2025
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /*
  4.  
  5. given you a tree
  6. each node containing a character
  7. find count of all the vertical paths start from root node
  8. which can be formed as a palindrome
  9.  */
  10. /**
  11.  important point to understand here -
  12.  
  13. assume you have a tree like below
  14.  
  15.  
  16.                         a
  17.                         |
  18.                         a
  19.                        / \
  20.                       c   d
  21.                           |
  22.                           f
  23.                           |
  24.                           d
  25. the valid paths from root node that can become palindrome are
  26.  
  27. a
  28. a a
  29. a a d
  30. a a d f d
  31. a a c
  32.  
  33. with simple dfs and hashmap , you can easily solve this
  34. as you traverse the children of the node
  35. pass the current hasmap with curr node freq added
  36. when curr node has odd freq count <=1
  37. then the current path is valid otherwise go on ...
  38.  
  39.  
  40.  */
  41.  
  42. /*
  43.  
  44. //inputs for node characters and tree connections
  45. a a c d f d
  46. 6 5
  47. 0 1
  48. 1 2
  49. 1 3
  50. 3 4
  51. 4 5
  52.  
  53. expected output -
  54. The result is 5
  55.  
  56.  
  57.  */
  58.  
  59.  
  60. @SuppressWarnings({"unused","unchecked"})
  61. public class A20250306a_DEshaw  {
  62.  
  63.     static Scanner sc = new Scanner(System.in);
  64.  
  65.     private static int[] getArray() {
  66.         String[] sArr = sc.nextLine().split(" ");
  67.         int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
  68.         return arr;
  69.     }
  70.  
  71.     private static char[] getCharArray() {
  72.         String[] sArr = sc.nextLine().split(" ");
  73.         char[] cArr = new char[sArr.length];
  74.         for (int i = 0; i < sArr.length; i++) {
  75.             cArr[i] = sArr[i].charAt(0); // Take the first character of each string
  76.         }
  77.         return cArr;
  78.     }
  79.  
  80.     private static int getMax(int[] arr) {
  81.         int currMax = Integer.MIN_VALUE;
  82.         for (int curr : arr) {
  83.             currMax = Math.max(currMax, curr);
  84.         }
  85.         return currMax;
  86.     }
  87.  
  88.     public static void getCount(int currInd,ArrayList<Integer>[] adjList,HashMap<Character,Integer> fMap,
  89.     int[] ans,
  90.     char[] charValues,
  91.     int[] visited){
  92.         char currChar = charValues[currInd];
  93.         int currFreq = fMap.getOrDefault(currChar,0) +1;
  94.         if(currFreq%2==0){//that means odd became even
  95.             ans[1] -= 1;
  96.         }else{//even became now odd
  97.             ans[1] += 1;
  98.         }
  99.         if(ans[1]<=1){// odd count is in control , we still can make a palindrome
  100.             ans[0] += 1;
  101.         }
  102.         fMap.put(currChar,currFreq);
  103.         // System.out.println("map is "+fMap);
  104.         // System.out.println("oddcount is"+ ans[1]+"  for currChar "+currChar);
  105.  
  106.         // System.out.println("ans is"+ ans[0]);
  107.         visited[currInd] = 1;
  108.         for(int child:adjList[currInd]){
  109.             if(visited[child] == 1){
  110.                 continue;
  111.             }
  112.             getCount(child,adjList,fMap,ans,charValues,visited);
  113.         }
  114.         visited[currInd] = 0;
  115.         currFreq-=1;
  116.         //it is important to correct the oddCount too
  117.         if(currFreq%2==0){//that means odd became even
  118.             ans[1] -= 1;
  119.         }else{//even became now odd
  120.             ans[1] += 1;
  121.         }
  122.         fMap.put(currChar,currFreq);
  123.         return;
  124.     }
  125.    
  126.     public static void main(String args[]) {
  127.         // prepare the inputs
  128.  
  129.  
  130.         // int[] visitable = getArray();
  131.         char[] charValues = getCharArray();
  132.         int[] tempArr = getArray();
  133.  
  134.         int nodes = tempArr[0];
  135.         int edges = tempArr[1];
  136.         ArrayList<Integer>[] adjList =(ArrayList<Integer>[]) new ArrayList[nodes];//explicit type conversion
  137.         for(int i =0;i<nodes;i+=1){
  138.             adjList[i] = new ArrayList<>();
  139.         }
  140.         int temp = edges;
  141.         //fill up the graph
  142.         while(temp!=0){
  143.             temp-=1;
  144.             tempArr = getArray();
  145.             int n1 = tempArr[0];
  146.             int n2 = tempArr[1];
  147.             adjList[n1].add(n2);
  148.             adjList[n2].add(n1);
  149.         }
  150.         HashMap<Character,Integer> fMap = new HashMap<>();
  151.         int[] visited = new int[nodes];
  152.         int[] ans = new int[2];//1st index to maintain ans second to maintain odd freq count
  153.         getCount(0,adjList,fMap,ans,charValues,visited);
  154.         int res = ans[0];
  155.  
  156.  
  157.         System.out.println("The result is " + res);
  158.  
  159.     }
  160. }
  161.  
  162. class Pair{
  163.     int row;
  164.     int col;
  165.     public Pair(int i,int j){
  166.         this.row = i;
  167.         this.col = j;
  168.        
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement