diff --git a/medium/move_pieces_to_obtain_a_string.py b/medium/move_pieces_to_obtain_a_string.py new file mode 100644 index 0000000..34747dc --- /dev/null +++ b/medium/move_pieces_to_obtain_a_string.py @@ -0,0 +1,27 @@ +# https://leetcode.com/problems/move-pieces-to-obtain-a-string + +class Solution: + def canChange(self, start: str, target: str) -> bool: + length = len(start) + start_char_list = [] + target_char_list = [] + for i in range(length): + if start[i] != '_': + start_char_list.append([start[i], i]) + if target[i] != '_': + target_char_list.append([target[i], i]) + + if len(start_char_list) != len(target_char_list): + return False + + while len(start_char_list) > 0: + start_char, start_index = start_char_list.pop() + target_char, target_index = target_char_list.pop() + not_okay = start_char != target_char or \ + (start_char == 'L' and start_index < target_index) or \ + (start_char == 'R' and start_index > target_index) + if not_okay: + return False + + return True +