Approach to solve question leetcode — 17 Letter Combinations of a Phone Number

- For solving the problem recursively, we need helper function , in which digits, result and empty string (current string) will be passed as well as there will be a function which will return corresponding characters for each digit.
- for each number in digits , we will get possible characters of that number (for 2 ‘abc’ , for 3 ‘def’ like that).
- We will add each possible character into the current string and call the function recursively starting form next digit.
- then we cut the current string from end otherwise it will be added in next steps recursively.

Code:

Approach to solve leetcode question- 16 3Sum Closest

- This question is somewhat similar to previous question leetcode -15 3Sum with a small change.

2. In this solution , we will take each element of array nums , and then for each element we will take two elements ahead of that element. Find the total of all three and compare which one is closer to the target.

3.return the closest distance from target.

Code :

Approach to solve leetcode question-15 3Sum

We can get all combinations of 3 elements and see their sum is equal to 0 or not and return all combinations which sums to 0, but this approach will not be efficient.

Instead we will use following approach:

- we will sort the array first.

2. we will take each element of array and for elements after this , we will choose two elements , calculate sum of all elements and if it is 0 , add all elements in result array . It will be clear in code.

Code :

Approach to solve leetcode 12 question — Integer to Roman

- This question is quite easy to solve , we just have to declare mappings properly .
- After that, we will loop through the mapping and keep decrementing number if it exists in mapping and simultaneously adding corresponding roman into the result.
- return the result in end

Code:

Approach to solve leetcode 11 — Container With Most Water

- In this question, we are asked to calculate area between two bars, that will be calculated as (distance between bars)*(height of smaller bar).
- we will start two pointers one from start and other from end , and keep calculating area…