def main(): CH.printHeader("Valid Anagram") print("s = ", end="") s = input() print("t = ", end="") t = input() print(isAnagram(s, t))
if len(strs) == 0: return "" prefix = self.smallestWord(strs) match = True while len(prefix) > 0: for i in strs: if prefix == i[:len(prefix)]: match = True continue else: match = False break if not match: prefix = prefix[:len(prefix) - 1] else: break return prefix if match else "" if __name__ == "__main__": printHeader("Longest Common Prefix") my_solution = Solution() s = ["flower", "flow", "flight"] # s = ["dog","racecar","car"] print(my_solution.longestCommonPrefix(s))
from challenge_header import printHeader def findTwoSum(nums, target) -> None: two_indices = [] for i in range(0, len(nums)): diff = target - nums[i] nums_left = nums[i + 1:] if diff in nums_left: two_indices.append(i) two_indices.append(nums_left.index(diff) + i + 1) break else: continue print(two_indices) if __name__ == "__main__": printHeader("Two Sum") nums = [3, 6, 3] target = 6 findTwoSum(nums, target)
def main(): CH.printHeader("Merge Sorted Array") nums1 = [1, 2, 3, 0, 0, 0] nums2 = [2, 5, 6] merge(nums1, 3, nums2, len(nums2))
from challenge_header import printHeader def containsDuplicate(nums) -> bool: nums_set = set(nums) return False if len(nums_set) == len(nums) else True if __name__ == "__main__": printHeader("Contains Duplicate") nums = [1, 2, 3, 1] print(containsDuplicate(nums))
import challenge_header as CH def singleNumber(num_list) -> int: nums_set = set(num_list) for i in nums_set: if num_list.count(i) == 1: return i else: continue if __name__ == "__main__": CH.printHeader("Single Number") num_list = [4, 1, 2, 1, 2] lone_num = singleNumber(num_list) print(lone_num if lone_num != -1 else "All numbers repeat")
col = 0 while col < 9: row = 0 while row < 9: sub_box = [board[row][col],board[row][col+1],board[row][col+2], board[row+1][col],board[row+1][col+1],board[row+1][col+2], board[row+2][col],board[row+2][col+1],board[row+2][col+2]] sub_box_validity_check = sub_box_validity_check and self.isSubBoxValid(sub_box) row += 3 col += 3 return row_validity_check and col_validity_check and sub_box_validity_check if __name__ == "__main__": printHeader("Valid Sudoku") board = [["5","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"]] my_solution = Solution() print(my_solution.isValidSudoku(board))
import challenge_header as CH def isIsomorphic(s: str, t: str) -> bool: return False if __name__ == "__main__": CH.printHeader("Isomorphic Strings") s = 'paper' t = 'title' result = isIsomorphic(s, t) print(f"\n{s} <--> {t}\nIsomorphic? {result}\n")
from challenge_header import printHeader class Solution: def __init__(self): pass def isValidPalindrome(self, s: str) -> bool: s_alpha_only = [i.lower() for i in s if i.isalnum()] salpha_len = len(s_alpha_only) for i in range(salpha_len): if s_alpha_only[i] != s_alpha_only[salpha_len - 1 - i]: return False return True if __name__ == "__main__": printHeader("Valid Palindrome") my_solution = Solution() # s = "A man, a plan, a canal: Panama" s = "0P" print(my_solution.isValidPalindrome(s))