Problem Set of Leetcode Java
Problem Set of Leetcode Java
j++;
if (node.children[i] == null) {
return word;
} else if (node.children[i].isEnd) {
return word.substring(0, j);
} else {
node = node.children[i];
}
}
return word;
}
class Trie {
Trie[] children;
boolean isEnd;
public Trie() {
children = new Trie[26];
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
isEnd = false;
}
}
Output:
Output:
}
public String search(String word) {
Trie node = root;
int j = 0;
for (char c : word.toCharArray()) {
int i = c - 'a';
j++;
if (node.children[i] == null) {
return word;
} else if (node.children[i].isEnd) {
return word.substring(0, j);
} else {
node = node.children[i];
}
}
return word;
}
class Trie {
Trie[] children;
boolean isEnd;
public Trie() {
children = new Trie[26];
isEnd = false;
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Problem 4: Combinations
Solution:
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> comb = new ArrayList<>();
backtrack(1, comb, res, n, k);
return res;
}
Output: