diff --git a/AlienDictionary.class b/AlienDictionary.class new file mode 100644 index 0000000..7fe949e Binary files /dev/null and b/AlienDictionary.class differ 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 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 map; + public boolean isAlienSorted(String[] words, String order) { + if(words==null || words.length==0){ + return true; + } + map = new HashMap<>(); + for(int i=0;i