0% found this document useful (0 votes)
16 views

Problem Set of Leetcode Java

Problem of leeetcode

Uploaded by

Saurav Singha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Problem Set of Leetcode Java

Problem of leeetcode

Uploaded by

Saurav Singha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Winning Camp Worksheet (Java)


Day: 20
Name : Saurav Singha UID : 21BCS5421
Subject : Java Section : SC-904 (B)
Date : 20/06/2024

Problem 1: Replace Words


Solution:
class Solution {
Trie root;

public String replaceWords(List<String> dictionary, String sentence) {


root = new Trie();
for (String word : dictionary) {
insert(word);
}

StringBuilder result = new StringBuilder();


String[] input = sentence.split(" ");
for (String i : input) {
result.append(search(i));
result.append(" ");
}
return result.toString().trim();
}

public String search(String word) {


Trie node = root;
int j = 0;
for (char c : word.toCharArray()) {
int i = c - 'a';
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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;
}

public void insert(String word) {


Trie node = root;
for (char c : word.toCharArray()) {
int i = c - 'a';
if (node.children[i] == null) {
node.children[i] = new Trie();
}
node = node.children[i];
}
node.isEnd = true;
}
}

class Trie {
Trie[] children;
boolean isEnd;

public Trie() {
children = new Trie[26];
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

isEnd = false;
}
}
Output:

Problem 2: Rotate String


Solution:
class Solution {
public boolean rotateString(String s, String goal) {
return s.length() == goal.length() && (s + s).contains(goal);
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Problem 3: First Missing positive


Solution:
class Solution {
Trie root;
public String replaceWords(List<String> dictionary, String sentence) {
root = new Trie();
for (String word : dictionary) {
insert(word);
}
StringBuilder result = new StringBuilder();
String[] input = sentence.split(" ");
for (String i : input) {
result.append(search(i));
result.append(" ");
}
return result.toString().trim();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
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;
}

public void insert(String word) {


Trie node = root;
for (char c : word.toCharArray()) {
int i = c - 'a';
if (node.children[i] == null) {
node.children[i] = new Trie();
}
node = node.children[i];
}
node.isEnd = true;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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;
}

private void backtrack(int start, List<Integer> comb, List<List<Integer>> res, int


n, int k) {
if (comb.size() == k) {
res.add(new ArrayList<>(comb));
return;
}

for (int num = start; num <= n; num++) {


comb.add(num);
backtrack(num + 1, comb, res, n, k);
comb.remove(comb.size() - 1);
}
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

You might also like