Skip to content

Commit

Permalink
Added codes for 13 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 13, 2024
1 parent 53d0ab8 commit 8deded2
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
176 changes: 176 additions & 0 deletions GeeksForGeeks/February/13-2-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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<Node> neighbors;
public Node(){
val = 0;
neighbors = new ArrayList<>();
}

public Node(int val){
this.val = val;
neighbors = new ArrayList<>();
}

public Node(int val, ArrayList<Node> 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<Node> bfs(Node src){
ArrayList<Node> ans = new ArrayList<>();
HashSet<Node> visited = new HashSet<>();
Queue<Node> q = new LinkedList<>();
q.add(src);
visited.add(src);

while(!q.isEmpty()){
Node u = q.poll();
ans.add(u);
ArrayList<Node> 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<Node> prevAns = bfs(prev);
ArrayList<Node> 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<Node> li = new ArrayList<>();
String arr[] = sc.nextLine().split(" ");
for(String s : arr){
li.add(v[Integer.parseInt(s)]);
}
v[i].neighbors = li;
}
ArrayList<Node> prev = bfs(v[0]);
Node ans = ob.cloneGraph(v[0]);
ArrayList<Node> 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<Node> neighbors;
public Node(){
val = 0;
neighbors = new ArrayList<>();
}
public Node(int val){
this.val = val;
neighbors = new ArrayList<>();
}
public Node(int val, ArrayList<Node> neighbors){
this.val = val;
this.neighbors = neighbors;
}
}
*/
class Solution
{
Node cloneGraph(Node source)
{
Queue<Node> q = new LinkedList<Node>();
q.add(source);

HashMap<Node,Node> hm = new HashMap<Node,Node>();

hm.put(source,new Node(source.val));

while (!q.isEmpty())
{
Node u = q.poll();

Node cloneNodeU = hm.get(u);
if (u.neighbors != null)
{
ArrayList<Node> 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);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/13-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n+m)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/February/13-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(|word|)
33 changes: 33 additions & 0 deletions LeetCode/February/13-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 8deded2

Please sign in to comment.