From d3e518432ed2f8a882db5eb15aeb8cdcee2f9c71 Mon Sep 17 00:00:00 2001 From: Piyush Chhoriya Date: Tue, 8 Apr 2025 20:40:36 -0400 Subject: [PATCH 1/3] Find celebrity --- Find-Celebrity.java | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Find-Celebrity.java diff --git a/Find-Celebrity.java b/Find-Celebrity.java new file mode 100644 index 0000000..7edfe33 --- /dev/null +++ b/Find-Celebrity.java @@ -0,0 +1,81 @@ +## Problem 2:Find Celebrity(https://leetcode.com/problems/find-the-celebrity/) + +In this question we have to find out the celebrity from n people and we are given a condition that +- all people except celebrity knows the celebrity +- celebrity does not know the people +and to find out who knows whom we are given a function knows(a,b) which returns a true is a knows b and flase id a does not know b + +so this question can be solved in 2 ways +1. bruteforce +in this we will maintain and indegrees array so a celebrity will have n-1 indegrees so if knows(a,b) = true so we will do +indegre[a]-- and indegree[b]++ and at the end if indegree[i]==n-1 then it is the celebrity +Time Complexity : O(n2) +Space Complexity : O(n) +public class Solution extends Relation { + + public int findCelebrity(int n) { + + if(n==0){ + return -1; + } + int[] indegree = new int[n]; + for(int i=0;i Date: Thu, 10 Apr 2025 03:09:37 -0400 Subject: [PATCH 2/3] Alien Dictionary --- AlienDictionary.class | Bin 0 -> 2231 bytes AlienDictionary.java | 83 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 AlienDictionary.class create mode 100644 AlienDictionary.java diff --git a/AlienDictionary.class b/AlienDictionary.class new file mode 100644 index 0000000000000000000000000000000000000000..7fe949e2339353582e1e368dbaf0a0e78e5f79e0 GIT binary patch literal 2231 zcmZuyU2qds6#j0qn@!U#Ep3<5TI9c^P1{(03N}zJ1*FhYFifd+$Bp`OZ1tJ^S#< zcZ&c9@O}tB_%$dxR0IU-u9-K?Xv%b^qN9`7tb{8NIA}Yz+b`e`w~qx86o7`1zG?+euneIm;68T^tIb2^%#u>uA9XYgoqJob61JWy*4<+^Zf-@AxJiFJdz{5?9UK z0OL@?L+yhhY{fPW+jZ=~PO?}HcaX=-glpvlv>RrsV2xf8*ccvcFA?=pvT`P-Vb`*? zmw|y?&YT{$^KJ-j@XJJW=!l?`e6j^PKM*d1DP=bH{7Sj6r9%>`TSpWx5o*dJR9gk8 z3WgeX3#?mBvLsiH9_-bz54{94lSu)i64(U|2Lu|+ZJp~@vJ~EaDVLZ)(`xEUH8@P1xxN6(Qy>Vn14HeG@W&& zR>JKUgVlA!qRX>#}p{$vg;V1&bwBc;bf$qjFNclO!PGM?-JEA(?qSoH67Q% z90e(s(HsjRuq%AAOlwOF$^%#-V@!VBo^njL zkYn=ClpL3LiGyWaRxec9IjOVChx%jvRGu=JNfNqt*mkT_h4iGAJ8Mp+xKa~$&BS#! zqNQ&oP1y--s2DzphT@q*E@2(BrE%jkjjCG;6kC{4A3LRd*v}x4_r6??>{A^5eAqg< zQ{Ze9-)xXjJMKYz#fiWTj&fUq2w5ABJnH)e3W7Ya82J&w$l`4T>a{x^3#b~uhw8h~ zJL+2Qhwh@@f2cLGIEz@Mb@wTMuVN@&MQoVG&MreKqP17;_y)S6ev2&&XrpPki1za{ zikN%)+l-In51ze}oJC$DL|T=J-F=F`S2ffU(3qh%6N!++U4|+l1I;28pJ0=Rq;%g#L+8rQJYJ;_FPNV?=P@X78`GS;Ch!1Ti#XYg zJaj`@Vc4hodjm$GS)E5*U={<-su7sSm~YumD<_^DUHPK_+Rml;Jl_F^Ge7rqhPfJN zlBAy^-kkM{CD-nFb(J;#lKoy*x{AL?onsS1*oAe407Aoz=+?)bV=J7rncq9 z2nW7L)40F>t@yZ7KM@~SIU8O;4dpO7@--5jks|CO(yKS6o|eFI=4FVnpJ4V*67we4 He0cAF9w`Yq literal 0 HcmV?d00001 diff --git a/AlienDictionary.java b/AlienDictionary.java new file mode 100644 index 0000000..7a1ce10 --- /dev/null +++ b/AlienDictionary.java @@ -0,0 +1,83 @@ +Time Complexity : O(N*k) -> N = no of words and k is the average length +Space Complexity : O(N*k) +In this as we want to find a order and when we comapared 2 strings we found out a relationship that c1 comes before c2 and so on so we can say +that it is a graph problem. Now what we will do is first iterate through all the words and find unique characters and add them in a map +Then we will find out the relationship ones we get the mismatching character in both the strings then we will add them in a map and indegrees array and breaks. +then we will iterate and find out the character with 0 indegree and add it in a queue and carry out BFS. The popped character is stored in StringBuilder + +import java.util.*; +class AlienDictionary{ + HashMap> map; + int[] indegree; + public String getOrder(String[] str){ + if(str==null || str.length==0){ + return ""; + } + map = new HashMap<>(); + indegree = new int[26]; + for(int j=0;j()); + } + } + + for(int i=0;i q = new LinkedList<>(); + StringBuilder sb = new StringBuilder(); + for(int i=0;i<26;i++){ + if(map.containsKey((char)('a'+i)) && indegree[i]==0){ + q.add((char)('a'+i)); + } + } + // System.out.println(q.peek()); + while(!q.isEmpty()){ + char ch = q.poll(); + sb.append(ch); + List list = map.get(ch); + for(int i=0;i> entry : map.entrySet()){ + // System.out.println(entry.getKey() +""+ entry.getValue()); + // } + // for(int i=0;i<26;i++){ + // if(indegree[i]!=0){ + // System.out.println(i + " "+indegree[i]); + // } + // } + // return ""; + + } + + public static void main(String[] args){ + String[] str = {"wrt","wrf","er","ett","rfttz"}; + // String[] str = {"z","a","z"}; + AlienDictionary ad = new AlienDictionary(); + System.out.println(ad.getOrder(str)); + } +} \ No newline at end of file From c496ca8a76c0f7b64a3622be1a9f2c9119a3a5cd Mon Sep 17 00:00:00 2001 From: Piyush Chhoriya Date: Thu, 10 Apr 2025 03:16:08 -0400 Subject: [PATCH 3/3] Alien Dictionary --- Verifying-an-Alien-Dictionary.java | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Verifying-an-Alien-Dictionary.java diff --git a/Verifying-an-Alien-Dictionary.java b/Verifying-an-Alien-Dictionary.java new file mode 100644 index 0000000..741dbd2 --- /dev/null +++ b/Verifying-an-Alien-Dictionary.java @@ -0,0 +1,32 @@ +class Solution { + HashMap map; + public boolean isAlienSorted(String[] words, String order) { + if(words==null || words.length==0){ + return true; + } + map = new HashMap<>(); + for(int i=0;i