0% found this document useful (0 votes)
4 views4 pages

LeetCode-ProblemList

The document provides solutions for three coding problems: merging two strings alternately, finding the greatest common divisor of two strings, and determining which kids can have the greatest number of candies after receiving extra candies. Each problem includes example inputs and outputs, along with Java code implementations. The solutions utilize string manipulation and array operations to achieve the desired results.

Uploaded by

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

LeetCode-ProblemList

The document provides solutions for three coding problems: merging two strings alternately, finding the greatest common divisor of two strings, and determining which kids can have the greatest number of candies after receiving extra candies. Each problem includes example inputs and outputs, along with Java code implementations. The solutions utilize string manipulation and array operations to achieve the desired results.

Uploaded by

19nitin3394
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Merged String Alternativly


=>
You are given two strings word1 and word2. Merge the strings by adding letters in
alternating order, starting with word1. If a string is longer than the other,
append the additional letters onto the end of the merged string.
Return the merged string.

Example 1:
Input: word1 = "abc", word2 = "pqr"
Output: "apbqcr"
Explanation: The merged string will be merged as so:
word1: a b c
word2: p q r
merged: a p b q c r

Example 2:
Input: word1 = "ab", word2 = "pqrs"
Output: "apbqrs"
Explanation: Notice that as word2 is longer, "rs" is appended to the end.
word1: a b
word2: p q r s
merged: a p b q r s

Example 3:
Input: word1 = "abcd", word2 = "pq"
Output: "apbqcd"
Explanation: Notice that as word1 is longer, "cd" is appended to the end.
word1: a b c d
word2: p q
merged: a p b q c d

**Solution
=>
class Solution {
public String mergeAlternately(String word1, String word2) {
char[] charArray1 = word1.toCharArray();
char[] charArray2 = word2.toCharArray();
int len = word1.length() > word2.length() ? word1.length() :
word2.length();
StringBuilder result = new StringBuilder();
for(int i=0;i<len;i++){
if(i < charArray1.length){
result.append(charArray1[i]);
}
if(i < charArray2.length){
result.append(charArray2[i]);
}
}
return result.toString();
}
}
-------------------------------------------------------------------------------
class Solution {
public String mergeAlternately(String word1, String word2) {
int len = word1.length() + word2.length();
Character[] res = new Character[len];
int index = 0;
for(int i=0;i<len;i++){
if(i < word1.length()){
res[index] = word1.charAt(i);
index++;
}
if(i < word2.length()){
res[index] = word2.charAt(i);
index++;
}
}
StringBuilder result = new StringBuilder();
for (Character ch : res) {
if (ch != null) {
result.append(ch);
}
}
return result.toString();
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------
**Greatest Common Divisor of Strings

Example 1:

Input: str1 = "ABCABC", str2 = "ABC"


Output: "ABC"
Example 2:

Input: str1 = "ABABAB", str2 = "ABAB"


Output: "AB"
Example 3:

Input: str1 = "LEET", str2 = "CODE"


Output: ""
Solution=>

class Solution {
public String gcdOfStrings(String str1, String str2) {
if(!(str1+str2).equals(str2+str1)){
return "";
}
int len = gcd(str1.length(), str2.length());
return str1.substring(0, len);
}
public int gcd(int len1, int len2){
while(len2 != 0){
int temp = len2;
len2 = len1 % len2;
len1 = temp;
}
return len1;
}
}
-----------------------------------------------------------------------------------
------------------------------------------------------------------
** Kids With the Greatest Number of Candies
There are n kids with candies. You are given an integer array candies, where each
candies[i] represents the number of candies the ith kid has, and an integer
extraCandies, denoting the number of extra candies that you have.
Return a boolean array result of length n, where result[i] is true if, after giving
the ith kid all the extraCandies, they will have the greatest number of candies
among all the kids, or false otherwise.
Note that multiple kids can have the greatest number of candies.

Example 1:
Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation: If you give all extraCandies to:
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the
kids.
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.

Example 2:
Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]
Explanation: There is only 1 extra candy.
Kid 1 will always have the greatest number of candies, even if a different kid is
given the extra candy.

Solution=>
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int greatestNo = Arrays.stream(candies).max().getAsInt();
List<Boolean> result = new ArrayList<Boolean>();
for(int i=0;i<candies.length;i++){
if(candies[i]+extraCandies >= greatestNo){
result.add(i, true);
}else{
result.add(i, false);
}
}
return result;
}
}
-----------------------------
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int mx = 0;
for (int candy : candies) {
mx = Math.max(mx, candy);
}
List<Boolean> res = new ArrayList<>();
for (int candy : candies) {
res.add(candy + extraCandies >= mx);
}
return res;
}
}
--------------------------------
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
List<Boolean> ans = new ArrayList<>();
int max = 0;
for(int i: candies){
if(i > max){
max = i;
}
}
for(int i : candies){
if(i + extraCandies >= max){
ans.add(true);
}else{
ans.add(false);
}
}
return ans;
}
}
-----------------------------------------------------------------------------------
------------------------------------------------------

You might also like