From 00eacff816e458cadb4442e7b2ec596e6bce1a88 Mon Sep 17 00:00:00 2001 From: Yitzchak Scott-Thoennes Date: Sun, 27 Jul 2025 21:03:54 -0400 Subject: challenge 331: fix task2 solutions --- challenge-331/ysth/python/ch-2.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'challenge-331/ysth/python') diff --git a/challenge-331/ysth/python/ch-2.py b/challenge-331/ysth/python/ch-2.py index 6d0f7c80e4..a87815fd24 100644 --- a/challenge-331/ysth/python/ch-2.py +++ b/challenge-331/ysth/python/ch-2.py @@ -3,7 +3,30 @@ import regex from itertools import batched def buddy_strings(string1: str, string2: str) -> bool: - return True if regex.fullmatch(regex.escape(string1)+'{1<=s<=1}', string2) else False + if len(string1) != len(string2): + return False + swaps = 0 + pair_exists: bool = False + previous_character1: str + previous_character2: str + for i in range(len(string1)): + character1: str = string1[i] + character2: str = string2[i] + if swaps == 1: + # second character of swap + if previous_character2 != character1 or character2 != previous_character1: + return False + swaps = 2 + elif string1[i] != string2[i]: + # starting the first swap + if swaps > 0: + return False + previous_character1 = character1 + previous_character2 = character2 + swaps = 1 + pair_exists = pair_exists or i > 0 and string1[i-1] == string1[i] + + return pair_exists and swaps == 0 or swaps == 2 def main() -> None: inputs: list[str] = sys.argv[1:] -- cgit