0% found this document useful (0 votes)
17 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)
17 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/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Winning Camp Worksheet (Java)


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

Problem 1: Additive Number Code


Solution:
class Solution {
public boolean isAdditiveNumber(String num) {
long number1 = 0;
for (int i = 0; i < num.length() - 1; i++) {
number1 = number1 * 10 + num.charAt(i) - '0';
long number2 = 0;
for (int j = i + 1; j < num.length(); j++) {
number2 = number2 * 10 + num.charAt(j) - '0';
if (solv(number1, number2, j + 1, num, 2))
return true;
if (number2 == 0)
break;
}
if (number1 == 0)
break;
}
return false;
}
boolean solv(long number1, long number2, int curr, String num, int count) {
if (curr >= num.length()) {
if (count >= 3)
return true;
return false;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
if (num.charAt(curr) == '0' && number1 + number2 != 0)
return false;
long number = 0;
long target = number1 + number2;
for (int i = curr; i < num.length(); i++) {
number = number * 10 + num.charAt(i) - '0';
if (number == target && solv(number2, target, i + 1, num, count + 1))
return true;
else if (number > target)
break;
}
return false;
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 2: Find the Index of the First Occurrence in a String Code


Solution:
class Solution {
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
}
Output:

Problem 3: Palindrome Partitioning Code


Solution:
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
List<String> path = new ArrayList<>();
partitionHelper(0, s, path, res);
return res;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

void partitionHelper(int index, String s, List<String> path, List<List<String>> res)


{
if (index == s.length()) {
res.add(new ArrayList<>(path));
return;
}
for (int i = index; i < s.length(); ++i) {
if (isPalindrome(s, index, i)) {
path.add(s.substring(index, i + 1));
partitionHelper(i + 1, s, path, res);
path.remove(path.size() - 1);
}
}
}
boolean isPalindrome(String s, int start, int end) {
while (start <= end) {
if (s.charAt(start) != s.charAt(end))
return false;
start++;
end--;
}
return true;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Problem 4: Pascal's Triangle Code


Solution:
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<>();
res.add(List.of(1));

for (int i = 0; i < numRows - 1; i++) {


List<Integer> dummyRow = new ArrayList<>();
dummyRow.add(0);
dummyRow.addAll(res.get(res.size() - 1));
dummyRow.add(0);
List<Integer> row = new ArrayList<>();

for (int j = 0; j < dummyRow.size() - 1; j++) {


row.add(dummyRow.get(j) + dummyRow.get(j + 1));
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

res.add(row);
}
return res;
}
}
Output:

You might also like