31 lines
926 B
Python
31 lines
926 B
Python
'''
|
|
Given a ticket string consisting of six digits, return YES if its lucky and NO in the opposite case. A ticket is considered lucky if the sum of the first
|
|
three digits is equal to the sum of the last three digits, even when there are leading zeroes.
|
|
|
|
Example 1
|
|
Input:
|
|
213132
|
|
Output:
|
|
YES
|
|
Example 2
|
|
Input:
|
|
973894
|
|
Output:
|
|
NO
|
|
'''
|
|
class Solution(object):
|
|
def solve(self,ticket:str)->str:
|
|
first_half = ticket[:3]
|
|
second_half = ticket[3:]
|
|
if sum(int(digit) for digit in first_half) == sum(int(digit) for digit in second_half):
|
|
return "YES"
|
|
return "NO"
|
|
|
|
# Example usage:
|
|
if __name__ == "__main__":
|
|
solution = Solution()
|
|
print(solution.solve("213132")) # Output: YES
|
|
print(solution.solve("973894")) # Output: NO
|
|
print(solution.solve("123321")) # Output: YES
|
|
print(solution.solve("000000")) # Output: YES
|
|
print(solution.solve("123456")) # Output: NO |