본문 바로가기
Problem Solving/Codility

[Python] StrSymmetryPoint

by 부르르 2021. 7. 18.

https://app.codility.com/demo/results/trainingYMXCBY-VNE/

 

Test results - Codility

Write a function: def solution(S) that, given a string S, returns the index (counting from 0) of a character such that the part of the string to the left of that character is a reversal of the part of the string to its right. The function should return −

app.codility.com


 

Point

  • center point까지 start와 end를 반복문을 통해 비교해간다

Tip

  •  

Task Score

100%

Correctness

100%

Performance

100%

Detected time complexity

O(length(S))

def solution(S):
    
    if len(S)%2 == 0:
        return -1

    mid = len(S)//2

    start = 0
    end = -1

    while start < mid:
        if S[start] != S[end]:
            return -1
        start += 1 
        end -= 1

    return mid

 

문제를 푸는데 도움이 된 테스트 케이스

 

 

728x90
반응형

'Problem Solving > Codility' 카테고리의 다른 글

[Python] LongestPassword  (0) 2021.07.18
[Python] TreeHeight  (0) 2021.07.18
[Python] FirstUnique  (0) 2021.07.15
[Python] DisappearingPairs  (0) 2021.07.15
[Python] 8.1. Dominator  (0) 2021.04.05

댓글