uebi.belli
BAN USERIt's not a well performed solution, but here we go:
public class Anagrams{
public static void main(String[] args){
new Anagrams().solution(new String[]{"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"});
}
private void solution(String[] args){
Map<String, List<String>> mapBase = new HashMap<>();
for(String word : args){
boolean found = false;
for(String key : mapBase.keySet()){
if(key.length() == word.length()){
int i=0;
for(; i<word.length(); i++){
if(key.indexOf(word.charAt(i)) == -1){
break;
}
}
if(i == word.length()){
found = true;
mapBase.get(key).add(word);
break;
}
}
}
if(!found){
mapBase.put(word, new ArrayList<>());
mapBase.get(word).add(word);
}
}
List<String> finalAnagrams = new ArrayList<>();
for(String key : mapBase.keySet()){
if(mapBase.get(key).size() > 1){
for(String word : mapBase.get(key)){
finalAnagrams.add(word);
}
}
}
System.out.println(finalAnagrams);
}
}
I would use the following:
I created a 2 level Map for type id search and id, with a List of workers inside. The server can only result all possible values of id, so it's not possible to do some "not in" search.
In that way, the cache process just check if there is a Workers list from some previous search and if don't, search in server and store results on server.
package com.test.carerrcup;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Worker {
private enum IndexTypes{
UNIT_ID,
DEPTH_ID
}
private Map<IndexTypes, Map<Integer, List<WorkerObj>>> cache;
public Worker(){
cache = new HashMap<>();
}
public List<WorkerObj> getWorkersByKey(IndexTypes indexType, Integer id){
List<WorkerObj> results = null;
Map<Integer, List<WorkerObj>> workersMap = cache.get(indexType);
if(cache.containsKey(indexType) && cache.get(indexType).containsKey(id)){
results = workersMap.get(id);
}
else{
results = getServerWorkers(indexType, id);
if(!cache.containsKey(indexType)){
cache.put(indexType, new HashMap<>());
}
if(!cache.get(indexType).containsKey(id)){
cache.get(indexType).put(id, results);
}
}
return results;
}
private List<WorkerObj> getServerWorkers(IndexTypes indexType, Integer id){
//return a lot of workers
return null;
}
}
class WorkerObj{
int workerId;
int deptId;
int unitId;
String Name;
String Surname;
String Data;
}
I guess that's it:
- uebi.belli April 20, 2016