CodeSignal-sringsRearrangement-Python

cosmos·2023년 4월 26일
0
post-thumbnail

문제

코드

from itertools import permutations
from typing import List

def check_str(str1: str, str2: str) -> bool:
    diff = 1
    
    if str1 == str2:
        return False
    
    for x in range(len(str1)):
        if str1[x] != str2[x]:
            diff -= 1
            if diff < 0:
                return False      
    return True

def solution(inputArray: List[str]) -> bool:
    for x in list(permutations(inputArray, len(inputArray))):
        correct = len(x) - 1
        for y in range(len(x)-1):
            if check_str(x[y], x[y+1]):
                correct -= 1
            if correct == 0:
                return True
    return False

코드 시간복잡도

시간 복잡도는 O(n! * n^2)

문제출처 & 깃허브

codesignal
github

0개의 댓글