문제 확인하기
2. 풀이 (Python, memory: 38708KB, time: 3244ms)
import sys
def calc_distance(p1, p2, p3):
distance = 0
for i in range(4):
distance += (1 if p1[i] != p2[i] else 0)
distance += (1 if p2[i] != p3[i] else 0)
distance += (1 if p3[i] != p1[i] else 0)
return distance
def find_closest_distance(N, mbti_list):
min_distance = float('inf')
for i in range(N):
for j in range(i + 1, N):
for k in range(j + 1, N):
distance = calc_distance(mbti_list[i], mbti_list[j], mbti_list[k])
min_distance = min(min_distance, distance)
if min_distance == 0:
return 0
return min_distance
def main():
T = int(input())
for _ in range(T):
N = int(input())
mbti_list = input().split()
result = find_closest_distance(N, mbti_list)
print(result)
if __name__ == '__main__':
main()