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

Problem Set of Leetcode Java

Problem of leeetcode

Uploaded by

Saurav Singha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Problem Set of Leetcode Java

Problem of leeetcode

Uploaded by

Saurav Singha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy