Skip to content

Commit 5fbfbc3

Browse files
authored
Merge pull request Sunchit#1055 from MaheshReddy-05/master
August DLC in Java
2 parents f9e9b2f + 861d267 commit 5fbfbc3

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// AUthor: Mahesh Reddy B N
2+
// Problem Link: https://leetcode.com/problems/combinations/description/
3+
4+
class Solution {
5+
public List<List<Integer>> combine(int n, int k) {
6+
List<List<Integer>> ls = new ArrayList<>();
7+
List<Integer> nums = new ArrayList<>();
8+
for(int i=1;i<=n;i++){
9+
nums.add(i);
10+
}
11+
List<Integer> al = new ArrayList<>();
12+
helper(ls,al,nums,0,k);
13+
return ls;
14+
}
15+
public void helper( List<List<Integer>> ls, List<Integer> al,List<Integer> nums,int si,int count){
16+
if(count<=0){
17+
ls.add(new ArrayList<>(al));
18+
return;
19+
}
20+
if(si>=nums.size()) return;
21+
al.add(nums.get(si));
22+
helper(ls,al,nums,si+1,count-1);
23+
al.remove(al.size()-1);
24+
helper(ls,al,nums,si+1,count);
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Author: Mahesh Reddy B N
2+
// Problem Link: https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
3+
4+
5+
class Solution {
6+
public List<String> letterCombinations(String digits) {
7+
String []str = new String[10];
8+
str[2] = "abc";
9+
str[3] = "def";
10+
str[4] = "ghi";
11+
str[5] = "jkl";
12+
str[6] = "mno";
13+
str[7] = "pqrs";
14+
str[8] = "tuv";
15+
str[9] = "wxyz";
16+
List<String> al = new ArrayList<>();
17+
helper(al,digits,str,0,"");
18+
return al;
19+
}
20+
public void helper(List<String> al,String s,String[] str,int si,String cur){
21+
if(si>= s.length()){
22+
if(cur.length()>0){
23+
al.add(cur);
24+
}
25+
return;
26+
}
27+
int curr = s.charAt(si) - '0';
28+
for(int i=0;i<str[curr].length();i++){
29+
String past = cur;
30+
cur+= str[curr].charAt(i);
31+
helper(al,s,str,si+1,cur);
32+
cur = past;
33+
}
34+
}
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Author: Mahesh Reddy B N
2+
// Problem Link: https://leetcode.com/problems/permutations/
3+
4+
class Solution {
5+
public List<List<Integer>> permute(int[] nums) {
6+
List<List<Integer>> ls = new ArrayList<>();
7+
List<Integer> al = new ArrayList<>();
8+
helper(ls,al,nums,0);
9+
return ls;
10+
}
11+
public void helper(List<List<Integer>> ls, List<Integer> al,int[] nums,int count){
12+
if(count>= nums.length){
13+
ls.add(new ArrayList<>(al));
14+
return;
15+
}
16+
for(int i=0;i<nums.length;i++){
17+
if(nums[i]!=-11){
18+
al.add(nums[i]);
19+
nums[i] = -11;
20+
helper(ls,al,nums,count+1);
21+
nums[i] = al.remove(al.size()-1);
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)