Skip to main content

Posts

Showing posts from June, 2022

Find Longest Substring Without Repeating Characters | Java and Python

Print Length of Longest Substring Without Repeating Characters from given String using Sliding Window Algorithm Problem Description : Given a string s, find the length of the longest substring without repeating characters. string consists of English letters, digits, symbols and spaces. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: s = " " (Space) Output: 1 Explanation: The answer is " ", with the length of 1. Example 4: Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Here we will use Sliding Window technique for solving this problem. We will also solve this prolem using using nested loop (Brute force technique). What is Sliding Window? Sliding Window is a computational technique which

Print Array Index of two sum equals to given Target from Sorted Array | Java and Python Solution

Two Sum Leetcode in Java and Python | Find pair of two values is equal to given Target Problem Description : 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[index 1 ] and numbers[index 2 ] where 1 <= index 1 < index 2 <= numbers.length . Return the indices of the two numbers, index 1 and index 2 , added by one as an integer array [index 1 , index 2 ] of length 2. The tests are generated such that there is exactly one solution . You may not use the same element twice. Example 1: Input: numbers = [ 2 , 7 ,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore, index 1 = 1, index 2 = 2. We return [1, 2]. Example 2: Input: numbers = [ 2 ,3, 4 ], target = 6 Output: [1,3] Explanation: The sum of 2 and 4 is 6. Therefore index 1 = 1, index 2 = 3. We return [1, 3]. Example 3: Input

How to rotate Array in Java with given n number without extra space?

Rotate Array LeetCode solution in Java with explanation | Rotate array without extra array Problem Description : Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1 : Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2 : Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100] Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] We have to rotate array based on given k number. We have multiple solution for this problem. Using Temporary array. Recursively rotate array one by one. Reverse array and swap the elements Here, we will seen 3rd solution. Solution 1 : Rotate array in java without extra space import java.util.Scanner; public class RotateArray {     public static void main(String[] args) {         Scanner sc = new Sca