Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added AlienDictionary.class
Binary file not shown.
83 changes: 83 additions & 0 deletions AlienDictionary.java
Original file line number Diff line number Diff line change
@@ -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<Character,List<Character>> 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<str.length;j++){
String s = str[j];
for(int i=0;i<s.length();i++){
char ch = s.charAt(i);
map.put(ch,new ArrayList<>());
}
}

for(int i=0;i<str.length-1;i++){
String str1 = str[i];
String str2 = str[i+1];
for(int j=0;j<str1.length() && j<str2.length();j++){
char ch1 = str1.charAt(j);
char ch2 = str2.charAt(j);
if(ch1 != ch2){
map.get(ch1).add(ch2);
indegree[ch2-'a']++;
break;
}
}
}

Queue<Character> 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<Character> list = map.get(ch);
for(int i=0;i<list.size();i++){
char c = list.get(i);
indegree[c-'a']--;
if(indegree[c-'a']==0){
q.add(c);
}
}
}
if(sb.toString().length() < map.size()){
return "";
}
return sb.toString();

// for(Map.Entry<Character,List<Character>> 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));
}
}
81 changes: 81 additions & 0 deletions Find-Celebrity.java
Original file line number Diff line number Diff line change
@@ -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<n;i++){
for(int j=0;j<n;j++){
if(i!=j){
if(knows(i,j)){
indegree[i]--;
indegree[j]++;
}
}
}

}
for(int i=0;i<n;i++){
if(indegree[i]==n-1){
return i;
}
}
return -1;

}


}

//Optimal Solution
In this what we will do is we will try to eliminate the people who are not the celebrities and find the potential celebrity and then we will check if he is the only celeberity
We will do this using 2 loops
1. will eliminate all other and find a potential celebrity
2. We will make sure he is the celebrity

Time Complexity : O(n)
Space Complexity : O(1)

public class Solution extends Relation {

public int findCelebrity(int n) {

if(n==0){
return -1;
}
int potential = 0;
//this will help us to find the potential celebrity
for(int i=1;i<n;i++){
if(knows(potential,i)){
potential = i;
}
}

//we will confirm if he is the celebrity
for(int i=0;i<n;i++){
if(i!=potential){
if(knows(potential,i) || !knows(i,potential)){
return -1;
}
}
}

return potential;

}
}
32 changes: 32 additions & 0 deletions Verifying-an-Alien-Dictionary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
HashMap<Character, Integer> map;
public boolean isAlienSorted(String[] words, String order) {
if(words==null || words.length==0){
return true;
}
map = new HashMap<>();
for(int i=0;i<order.length();i++){
char c = order.charAt(i);
map.put(c,i);
}
for(int i=0;i<words.length-1;i++){
boolean answer = checkLexicographical(words[i],words[i+1]);
if(answer == false){
return false;
}
}
return true;
}
private boolean checkLexicographical(String one, String two){
int m = one.length();
int n = two.length();
for(int i=0;i<m && i< n;i++){
char firstChar = one.charAt(i);
char secondChar = two.charAt(i);
if(map.get(firstChar) != map.get(secondChar)){
return map.get(firstChar) < map.get(secondChar);
}
}
return m<=n;
}
}