Reverse Prefix of Word

2Sum Problem Problem Given a 0-indexed string word and a character ch, reverse the segment of word that starts at index 0 and ends at the index of the first occurrence of ch (inclusive). If the character ch does not exist in word, do nothing. For example, if word = "abcdefd" and ch = "d", then you should reverse the segment that starts at 0 and ends at 3 (inclusive). The resulting string will be "dcbaefd". Return the resulting string. ...

Given a binary search tree with 2 nodes swapped find number of pairs not following BST properties

Given a binary search tree with 2 nodes swapped find number of pairs not following BST properties Problem Given a binary search tree with 2 nodes swapped find number of pairs not following BST properties Follow up: Recover Binary Search Tree Problem Examples Example 1 Consider the BST: 10 / \ 5 (8) / \ 2 (20) ...

Missing Number Problem

Problem Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Examples Example 1: Input: nums = [3,0,1] Output: 2 Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. ...

Palindrome Number Problem

Problem Determine whether an integer is a palindrome. Follow up Do this without extra space. Definition Palindrome Definition Examples Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left. Example 2: Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. ...

Balanced Binary Tree Problem

Problem Implement a function to check if a tree is balanced. Definition Height-balanced binary tree: is defined as a binary tree in which the depth of the two sub-trees of every node never differ by more than 1. Examples Example 1 3 / \ 9 20 / \ 15 7 ...

Buddy Strings Problem

Problem Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false. Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j]. For example, swapping at indices 0 and 2 in "abcd" results in "cbad". Examples Example 1: ...

Contains Duplicate 2 - within k distance

Problem Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k. Examples Example 1: Input nums = [1,2,3,1], k = 3 Output: true ...

Generate Nth Fibonacci Number

Problem The Fibonacci Numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, Sequence is defined as, $$ F_{0} = 0 $$ and $$ F_{1} = 1 $$ and $$ F_{n} = F_{n-1} + F_{n-2} $$ Given n, calculate F(n). Examples Example 1: Input : n = 2 Output : 1 ...

Longest Palindrome Problem

Problem Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome here. Examples Example 1: Input: s = "abccccdd" Output: 7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7. ...

Nim Game

Problem You are playing the following Nim Game with your friend: Initially, there is a heap of stones on the table. You and your friend will alternate taking turns, and you go first. On each turn, the person whose turn it is will remove 1 to 3 stones from the heap. The one who removes the last stone is the winner. Given n, the number of stones in the heap, return true if you can win the game assuming both you and your friend play optimally, otherwise return false. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy