diff --git a/GeeksForGeeks/February/13-2-24/GFG.java b/GeeksForGeeks/February/13-2-24/GFG.java new file mode 100644 index 0000000..ab7e818 --- /dev/null +++ b/GeeksForGeeks/February/13-2-24/GFG.java @@ -0,0 +1,176 @@ +//{ Driver Code Starts +//Initial Template for Java + +import java.io.*; +import java.util.*; +import java.math.*; + +class Node{ + int val; + ArrayList neighbors; + public Node(){ + val = 0; + neighbors = new ArrayList<>(); + } + + public Node(int val){ + this.val = val; + neighbors = new ArrayList<>(); + } + + public Node(int val, ArrayList neighbors){ + this.val = val; + this.neighbors = neighbors; + } +} + +class GFG{ + static class FastReader{ + BufferedReader br; + StringTokenizer st; + + public FastReader(){ + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next(){ + while (st == null || !st.hasMoreElements()){ + try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } + } + return st.nextToken(); + } + + String nextLine(){ + String str = ""; + try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } + return str; + } + + Integer nextInt(){ + return Integer.parseInt(next()); + } + } + + static ArrayList bfs(Node src){ + ArrayList ans = new ArrayList<>(); + HashSet visited = new HashSet<>(); + Queue q = new LinkedList<>(); + q.add(src); + visited.add(src); + + while(!q.isEmpty()){ + Node u = q.poll(); + ans.add(u); + ArrayList v = u.neighbors; + for(Node x : v){ + if(!visited.contains(x)){ + visited.add(x); + q.add(x); + } + } + } + + return ans; + } + + static boolean checkedClone(Node prev, Node new1){ + ArrayList prevAns = bfs(prev); + ArrayList newAns = bfs(new1); + for(int i = 0; i < prevAns.size(); i++){ + if(prevAns.get(i) == newAns.get(i)) + return false; + } + return true; + } + + public static void main(String[] args) throws IOException{ + FastReader sc = new FastReader(); + PrintWriter out = new PrintWriter(System.out); + int t = sc.nextInt(); + while(t-- > 0){ + int n = sc.nextInt(); + Node root = null; + Node v[] = new Node[n]; + for(int i = 0; i < n; i++) v[i] = new Node(i); + Solution ob = new Solution(); + for(int i = 0; i < n; i++){ + ArrayList li = new ArrayList<>(); + String arr[] = sc.nextLine().split(" "); + for(String s : arr){ + li.add(v[Integer.parseInt(s)]); + } + v[i].neighbors = li; + } + ArrayList prev = bfs(v[0]); + Node ans = ob.cloneGraph(v[0]); + ArrayList now = bfs(ans); + out.println(checkedClone(v[0], ans) ? "1" : "0"); + } + out.flush(); + } +} +// } Driver Code Ends + + +//User function Template for Java + + +/* + class Node{ + int val; + ArrayList neighbors; + public Node(){ + val = 0; + neighbors = new ArrayList<>(); + } + + public Node(int val){ + this.val = val; + neighbors = new ArrayList<>(); + } + + public Node(int val, ArrayList neighbors){ + this.val = val; + this.neighbors = neighbors; + } + } +*/ +class Solution +{ + Node cloneGraph(Node source) + { + Queue q = new LinkedList(); + q.add(source); + + HashMap hm = new HashMap(); + + hm.put(source,new Node(source.val)); + + while (!q.isEmpty()) + { + Node u = q.poll(); + + Node cloneNodeU = hm.get(u); + if (u.neighbors != null) + { + ArrayList v = u.neighbors; + for (Node graphNode : v) + { + Node cloneNodeG = hm.get(graphNode); + + if (cloneNodeG == null) + { + q.add(graphNode); + + cloneNodeG = new Node(graphNode.val); + hm.put(graphNode,cloneNodeG); + } + + cloneNodeU.neighbors.add(cloneNodeG); + } + } + } + + return hm.get(source); + } +} diff --git a/GeeksForGeeks/February/13-2-24/README.md b/GeeksForGeeks/February/13-2-24/README.md new file mode 100644 index 0000000..e95b48c --- /dev/null +++ b/GeeksForGeeks/February/13-2-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n+m) +Space complexity - O(n) diff --git a/LeetCode/February/13-2-24/README.md b/LeetCode/February/13-2-24/README.md new file mode 100644 index 0000000..365b6fb --- /dev/null +++ b/LeetCode/February/13-2-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(|word|) diff --git a/LeetCode/February/13-2-24/Solution.java b/LeetCode/February/13-2-24/Solution.java new file mode 100644 index 0000000..e33e9cb --- /dev/null +++ b/LeetCode/February/13-2-24/Solution.java @@ -0,0 +1,33 @@ +class Solution +{ + public String firstPalindrome(String[] words) + { + for (String word : words) + { + if (isPalindrome(word)) + return word; + } + + return ""; + } + + private boolean isPalindrome(String s) + { + int i = 0; + int j = s.length() -1; + + while (i <= j) + { + if (s.charAt(i) == s.charAt(j)) + { + i++; + j--; + } + else + { + return false; + } + } + return true; + } +}