Pergunta de entrevista da empresa Yelp
Main Technical Question:
Write a function that takes as input an array of words
input => ['cat', 'star', 'act', 'god', 'arts', 'dog', 'rats']
and returns a sorted array
output => ['cat', 'act', 'god', 'dog', 'start', 'arts', 'rats']
Respostas da entrevista
Follow senthill comment, I think they need another step of sorting based on the length.
words = ['cat', 'star', 'act', 'god', 'arts', 'dog', 'rats']
words = sorted(words,key=lambda x:sorted(x))
sorted(words, key=lambda x:len(x))
Below is a pythonic implementation of the same, assumption is we are allowed to built-in sorted method.
words = ['cat', 'star', 'act', 'god', 'arts', 'dog', 'rats']
print sorted(words,key=lambda x:sorted(x))
Below is a pythonic implementation of the same, assumption is we are allowed to built-in sorted method.
words = ['cat', 'star', 'act', 'god', 'arts', 'dog', 'rats']
print sorted(words,key=lambda x:sorted(x))
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Anagrams_Yelp {
public Anagrams_Yelp() {
String[] words = {"masterschool","cat", "star", "act", "god", "arts", "dog", "rats","schoolmaster"};
printAnagramsTogether(words);
}
private void printAnagramsTogether(String[] words) {
ArrayList finalList = new ArrayList();
HashMap> map = new HashMap>();
for (int i = 0; i list = map.get(myString);
list.add(str);
map.put(myString, list);
}else{
ArrayList list = new ArrayList();
list.add(str);
map.put(myString, list);
}
}
Set keySet = map.keySet();
Iterator objIterator = keySet.iterator();
while(objIterator.hasNext()){
ArrayList list = map.get(objIterator.next());
finalList.addAll(list);
}
int i = 0;
while(i
public static void insertionSort(String[] arr) {
for(int i=1;i arr[j+1].length()) {
String temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
return arr;
}
http://stackoverflow.com/questions/15045640/how-to-check-if-two-words-are-anagrams-in-java