Skip to main content

Posts

Showing posts from September, 2020

DECODE WAYS - LEETCODE PROBLEM 91

  A message containing letters from   A-Z   is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a  non-empty  string containing only digits, determine the total number of ways to decode it. Example 1: Input: "12" Output: 2 Explanation:  It could be decoded as "AB" (1 2) or "L" (12). Example 2: Input: "226" Output: 3 Explanation:  It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6). class Solution { public int numDecodings(String s) { int ways=0; ways=countDecoding(s.toCharArray(),s.length()); return ways; } int countDecoding(char[] digits, int n){ // System.out.println(" digits "+disgits[0]); // for base condition "01123" should return 0 // base cases if (n == 0 || (n == 1 && digits[0]!='

EDIT DISTANCE PROBLEM - LEETCODE 72

  Given two words  word1  and  word2 , find the minimum number of operations required to convert  word1  to  word2 . You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1 = "horse", word2 = "ros" Output: 3 Explanation: horse -> rorse (replace 'h' with 'r') rorse -> rose (remove 'r') rose -> ros (remove 'e') Solution:- class Solution { public int minDistance(String word1, String word2) { if(word1.length()==0){ return word2.length(); } if(word2.length()==0){ return word1.length(); } if(word1.length()==0 && word2.length()==0){ return 0; } int[][] result=new int[word1.length()+1][word2.length()+1]; for(int i=0;i<=word1.length();i++){ result[i][0]=i; }

TWO SUM -LEET CODE PROBLEM 1

   https://leetcode.com/problems/two-sum/ 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.   Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: 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] class Solution {     public int[] twoSum(int[] nums, int target) {              HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();                  for(int i=0;i<nums.length;i++){                          if(map.get(nums[i])==null){                 map.put(target-nums[i],i);             }             else{                 return new int[]{map.get(nums[i]),i};             }         }         return null

.