Author: devangini123

Problem Statement: Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Examples: Example 1: Input:haystack = “sadbutsad”, needle = “sad” Output:0 Explanation: “sad” occurs at index 0 and 6.The first occurrence is at index 0, so we return 0. Example 2: Input:haystack = “leetcode”, needle = “leeto” Output:-1 Explanation: “leeto” did not occur in “leetcode”, so we return -1. Constraints: 1

Read More

Problem Statement: Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets. Examples: Example 1: Input:nums = [-1,0,1,2,-1,-4] Output:[[-1,-1,2],[-1,0,1]] Explanation: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0. The distinct triplets are [-1,0,1] and [-1,-1,2]. Notice that…

Read More

Problem Statement: You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Notice that you may not slant the container. Examples: Example 1: Input:height = [1,8,6,2,5,4,8,3,7] Output:49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can…

Read More

Problem Statement: Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null. For example, the following two linked lists begin to intersect at node c1 The test cases are generated such that there are no cycles anywhere in the entire linked structure. Note that the linked lists must retain their original structure after the function returns. Custom Judge: The inputs to the judge are given as follows (your program is not given these inputs): intersectVal – The value of…

Read More

Problem Statement: Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Examples: Example 1: Input:haystack = “sadbutsad”, needle = “sad” Output:0 Explanation: “sad” occurs at index 0 and 6.The first occurrence is at index 0, so we return 0. Example 2: Input:haystack = “leetcode”, needle = “leeto” Output:-1 Explanation: “leeto” did not occur in “leetcode”, so we return -1. Constraints: 1

Read More

Problem Statement: Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., “ace” is a subsequence of “abcde” while “aec” is not). Examples: Example 1: Input: s = “abc”, t = “ahbgdc” Output: true Example 2: Input: s = “axc”, t = “ahbgdc” Output: false Constraints: 0

Read More

Problem Statement: Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 target: j -= 1 elif sum < target: i += 1 else: return [i + 1, j + 1] return [] class Solution { public int[] twoSum(int[] numbers, int target) { int i = 0; int j = numbers.length - 1; while(i < j) { int sum = numbers[i] + numbers[j]; if(sum > target) { –j; } else if(sum < target)…

Read More

🌙 Problem Statement: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Examples: Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2] Example 3: Input: nums = [3,3], target = 6 Output: [0,1] Constraints:…

Read More

Problem Statement: You are given an m x n grid where each cell can have one of three values: 0 representing an empty cell, 1 representing a fresh orange, or 2 representing a rotten orange. Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1. Examples: Example 1: Input: grid = [[2,1,1],[0,1,1],[1,0,1]] Output: -1 Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens…

Read More

Problem Statement: Given a circular integer array nums (i.e., the next element of nums[nums.length – 1] is nums[0]), return the next greater number for every element in nums. The next greater number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn’t exist, return -1 for this number. Examples: Example 1: Input: nums = [1,2,1] Output: [2,-1,2] Explanation:The first 1’s next greater number is 2; The number 2 can’t find next greater number. The second 1’s next greater number…

Read More