Remove Duplicates from Sorted List

Here is the link to the problem: Remove duplicates (Linked List). Problem Statement: Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Examples: Example 1: Input: head = [1,1,2] Output: [1,2] Example 2: Input: head = [1,1,2,3,3] Output: [1,2,3] My Solution # Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self....

August 19, 2024

Valid Sudoku

Here is the link to the problem: Valid Sudoku. Problem Statement: Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition....

August 19, 2024

Majority Element

Here is the link to the problem: Majority Element. Problem Statement: Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. Examples: Example 1: Input: nums = [3,2,3] Output: 3 Example 2: Input: nums = [2,2,1,1,1,2,2] Output: 2 My Solution class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ new_dict = {} for each in nums: if each in new_dict: new_dict[each] += 1 else: new_dict[each] = 1 for each in new_dict: if new_dict[each] > len(nums)/ 2: return each This is nothing, very simple....

July 26, 2024

Single Number

Here is the link to the problem: Single Number. Problem Statement: Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. Examples: Example 1: Input: nums = [2,2,1] Output: 1 Example 2: Input: nums = [4,1,2,1,2] Output: 4 Example 3: Input: nums = [1] Output: 1...

July 26, 2024

Valid Palindrome

Here is the link to the problem: Valid Palindrome. Problem Statement: A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. Examples: Example 1: Input: s = “A man, a plan, a canal: Panama”...

July 25, 2024

Merge sorted array

Here is the link to the problem: Merge Sorted Array. Problem Statement: You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1....

July 24, 2024

Climbing Stairs

Here is the link to the problem: Climbing Stairs. Problem Statement: You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Examples: Example 1: Input: n = 2 Output: 2 Explanation: There are two ways to climb to the top. 1 step + 1 step 2 steps...

July 22, 2024

Square root

Here is the link to the problem: Square root. Problem Statement: Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. You must not use any built-in exponent function or operator. For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python. Examples: Example 1: Input: x = 4 Output: 2...

July 22, 2024

Add Binary

Here is the link to the problem: Add Binary. Problem Statement: Given two binary strings a and b, return their sum as a binary string. Examples: Example 1: Input: a = “11”, b = “1” Output: “100” Example 2: Input: a = “1010”, b = “1011” Output: “10101” My Solution class Solution(object): def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ c = 0 res = "" a_r, b_r = a[::-1], b[::-1] for i in range(max(len(a), len(b))): a_val = a_r[i] if i < len(a) else 0 b_val = b_r[i] if i < len(b) else 0 sum = c + int(a_val) + int(b_val) r = sum % 2 c = sum // 2 res += str(r) if c: return (str(c) + res[::-1]) else: return res[::-1] So even though this is an easy problem, but there are a lot of things to be considered....

July 18, 2024

Length of Last word

Here is the link to the problem: Length of Last word. Problem Statement: Given a string s consisting of words and spaces, return the length of the last word in the string. A word is a maximal substring consisting of non-space characters only. Examples: Example 1: Input: s = “Hello World” Output: 5 Explanation: The last word is “World” with length 5. Example 2: Input: s = " fly me to the moon "...

July 13, 2024