간단한 방법은 3 시퀀스를 취하고 HashTable
에 넣어하는 것입니다. 일련의 3을 만나면 해당 발생 카운터를 증가시킵니다. 결국 가장 빈번한 출현/시퀀스를 반환합니다. 이는 최대 출현 값을 가진 입력에 대해 HashTable
을 스캔하여 발견됩니다. Java의 예 :
public class Sequence {
public List<String> sequenceOfThree(List<List<String>> names){
Map<List<String>, Integer> map = new HashMap<List<String>, Integer>();
for(List<String> nameList:names){
int startIdx = 0;
int endIdx = 3;
while(endIdx <= nameList.size()){
List<String> subsequence = nameList.subList(startIdx, endIdx);
//add to map
Integer count = map.get(subsequence);
if(count == null){
count = 0;
}
map.put(subsequence, count + 1);
startIdx++;
endIdx++;
}
}
Integer max = Integer.MIN_VALUE;
List<String> result = Collections.emptyList();
for(Entry<List<String>, Integer> entries:map.entrySet()){
if(entries.getValue() > max){
max = entries.getValue();
result = entries.getKey();
}
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
List<List<String>> names = new ArrayList<List<String>>();
names.add(Arrays.asList(new String[]{"Ana", "John", "Maria"}));
names.add(Arrays.asList(new String[]{"Paul"}));
names.add(Arrays.asList(new String[]
"Sharon", "Ana", "John", "Maria", "Tiffany" ,"Ted"}));
System.out.println(new Sequence().sequenceOfThree(names));
}
}
각 단어를 세어보고 개수를 비교하면됩니다. 가장 우아한 해결책은 아니지만 아마도 가장 간단한 해결책 일 것입니다. – Hassan
@oleksii 길이 3의 시퀀스입니다. –
3 개의 이름 (-sequences)을 가진 배열입니까? 아니면 각각에 몇 개의 이름이있는 3 개의 배열입니까? – aefxx