-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'SnowScriptWinterOfCode:main' into patch-5
- Loading branch information
Showing
80 changed files
with
2,388 additions
and
66 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
Day 10/Q3 Merge two Sorted Linked list/Anushreebasics--java.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
``` | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
if(list1==null && list2==null){ | ||
return null; | ||
} | ||
if(list1==null){ | ||
return list2; | ||
} | ||
if(list2==null){ | ||
return list1; | ||
} | ||
ListNode ans=new ListNode(0); | ||
ListNode temp=ans; | ||
ListNode temp1=list1; | ||
ListNode temp2=list2; | ||
while(temp1!=null && temp2!=null){ | ||
if(temp1.val <= temp2.val){ | ||
temp.next=temp1; | ||
temp1=temp1.next; | ||
} | ||
else{ | ||
temp.next=temp2; | ||
temp2=temp2.next; | ||
} | ||
temp=temp.next; | ||
} | ||
if(temp1!=null){ | ||
temp.next=temp1; | ||
} | ||
if(temp2!=null){ | ||
temp.next=temp2; | ||
} | ||
ans=ans.next; | ||
return ans; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
``` | ||
/** | ||
* Definition for singly-linked list. | ||
* class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode(int x) { | ||
* val = x; | ||
* next = null; | ||
* } | ||
* } | ||
*/ | ||
public class Solution { | ||
public boolean hasCycle(ListNode head) { | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
``` | ||
class Solution { | ||
public: | ||
string longestPalindrome(string s) { | ||
if (s == string(s.rbegin(), s.rend())) { | ||
return s; | ||
} | ||
string left = longestPalindrome(s.substr(1)); | ||
string right = longestPalindrome(s.substr(0, s.size() - 1)); | ||
if (left.length() > right.length()) { | ||
return left; | ||
} else { | ||
return right; | ||
} | ||
} | ||
}; | ||
``` |
7 changes: 6 additions & 1 deletion
7
Day 13/Q2: Longest Palindromic Substring → ...Longest Palindromic Substring/question.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
Given a string s, return the longest palindromic substring in s. | ||
|
||
``` | ||
Example 1: | ||
Input: s = "babad" | ||
Output: "bab" | ||
Example 2: | ||
Input: s = "cbbd" | ||
Output: "bb" | ||
|
||
``` | ||
|
||
``` | ||
Constraints: | ||
1 <= s.length <= 1000 | ||
s consist of only digits and English letters. | ||
``` |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
``` | ||
class Solution { | ||
public: | ||
string longestPalindrome(string s) { | ||
if (s == string(s.rbegin(), s.rend())) { | ||
return s; | ||
} | ||
string left = longestPalindrome(s.substr(1)); | ||
string right = longestPalindrome(s.substr(0, s.size() - 1)); | ||
if (left.length() > right.length()) { | ||
return left; | ||
} else { | ||
return right; | ||
} | ||
} | ||
}; | ||
``` |
19 changes: 19 additions & 0 deletions
19
Day-1/q1-Product of Array Except Self/Anushreebasics--java.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
``` | ||
class Solution { | ||
public int[] productExceptSelf(int[] nums) { | ||
int n=nums.length; | ||
int[] pf=new int[n]; | ||
pf[0]=1; | ||
for(int i=1;i<n;i++){ | ||
pf[i]=pf[i-1]*nums[i-1]; | ||
} | ||
int synx=1; | ||
for(int i=n-1;i>=0;i--){ | ||
pf[i]=pf[i]*synx; | ||
synx=synx*nums[i]; | ||
} | ||
return pf; | ||
} | ||
} | ||
``` |
30 changes: 30 additions & 0 deletions
30
Day-1/q2-Number of pairs in an array/Anushreebasics--java.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
``` | ||
class Solution { | ||
public boolean binaSer(int[] arr, int s, int e, int k){ | ||
while(s<=e){ | ||
int mid=(s+e)/2; | ||
if(arr[mid]==k){ | ||
return true; | ||
} | ||
else if(arr[mid]>k){ | ||
e-=1; | ||
} | ||
else{ | ||
s+=1; | ||
} | ||
} | ||
return false; | ||
} | ||
public int findPairs(int[] nums, int k) { | ||
HashSet<Integer> qns=new HashSet<>(); | ||
Arrays.sort(nums); | ||
int n=nums.length; | ||
for(int i=0;i<n-1;i++){ | ||
if(binaSer(nums,i+1,n-1,nums[i]+k)){ | ||
qns.add(nums[i]); | ||
} | ||
} | ||
return qns.size(); | ||
} | ||
} | ||
``` |
50 changes: 50 additions & 0 deletions
50
Day-14/q3: Implement Stack using queues/Anushreebasics--java.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
``` | ||
class MyStack { | ||
Queue<Integer> q; | ||
public MyStack() { | ||
q=new LinkedList<>(); | ||
} | ||
public void push(int x) { | ||
if(q.size()==0){ | ||
q.add(x); | ||
return; | ||
} | ||
Queue<Integer> temp=new LinkedList<>(); | ||
while(q.size()>0){ | ||
temp.add(q.remove()); | ||
} | ||
q.add(x); | ||
while(temp.size()>0){ | ||
q.add(temp.remove()); | ||
} | ||
} | ||
public int pop() { | ||
if(q.size()>0){ | ||
return q.remove(); | ||
} | ||
return -1; | ||
} | ||
public int top() { | ||
return q.peek(); | ||
} | ||
public boolean empty() { | ||
if(q.size()>0){ | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
/** | ||
* Your MyStack object will be instantiated and called as such: | ||
* MyStack obj = new MyStack(); | ||
* obj.push(x); | ||
* int param_2 = obj.pop(); | ||
* int param_3 = obj.top(); | ||
* boolean param_4 = obj.empty(); | ||
*/ | ||
``` |
18 changes: 18 additions & 0 deletions
18
Day-15/q1-Lowest Common Ancestor of a Binary Tree/Akansha--C.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { | ||
if (!root || root == p || root == q) return root; | ||
TreeNode* left = lowestCommonAncestor(root->left, p, q); | ||
TreeNode* right = lowestCommonAncestor(root->right, p, q); | ||
return !left ? right : !right ? left : root; | ||
} | ||
}; |
16 changes: 16 additions & 0 deletions
16
Day-15/q3-Longest Increasing Subsequence/Anushreebasics--java.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
``` | ||
class Solution { | ||
public int lengthOfLIS(int[] nums) { | ||
int[] dp=new int[nums.length]; | ||
Arrays.fill(dp,1); | ||
for(int i=1;i<nums.length;i++){ | ||
for(int j=0;j<i;j++){ | ||
if(nums[j]<nums[i]){ | ||
dp[i]=Math.max(dp[i],dp[j]+1); | ||
} | ||
} | ||
} | ||
return Arrays.stream(dp).max().orElse(0); | ||
} | ||
} | ||
``` |
30 changes: 30 additions & 0 deletions
30
Day-17/Q1:Determine if two strings are close/Anushreebasics--java.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
``` | ||
class Solution { | ||
public boolean closeStrings(String word1, String word2) { | ||
if(word1.length()!=word2.length()){ | ||
return false; | ||
} | ||
int[] freq1=new int[26]; | ||
int[] freq2=new int[26]; | ||
for(int i=0;i<word1.length();i++){ | ||
freq1[word1.charAt(i)-'a']++; | ||
} | ||
for(int i=0;i<word2.length();i++){ | ||
freq2[word2.charAt(i)-'a']++; | ||
} | ||
for(int i=0;i<26;i++){ | ||
if((freq1[i]==0 && freq2[i]!=0) || (freq1[i]!=0 && freq2[i]==0)){ | ||
return false; | ||
} | ||
} | ||
Arrays.sort(freq1); | ||
Arrays.sort(freq2); | ||
for(int i=0;i<26;i++){ | ||
if(freq1[i]!=freq2[i]){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
``` |
28 changes: 28 additions & 0 deletions
28
Day-17/Q1:Determine if two strings are close/namita0210_java.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class namita0210_java { | ||
public boolean closeStrings(String word1, String word2) { | ||
if (word1.length() != word2.length()) { | ||
return false; | ||
} | ||
|
||
Map<Character, Integer> count1 = new HashMap<>(); | ||
Map<Character, Integer> count2 = new HashMap<>(); | ||
|
||
// Count occurrences of characters in word1 | ||
for (char ch : word1.toCharArray()) { | ||
count1.put(ch, count1.getOrDefault(ch, 0) + 1); | ||
} | ||
|
||
// Count occurrences of characters in word2 | ||
for (char ch : word2.toCharArray()) { | ||
count2.put(ch, count2.getOrDefault(ch, 0) + 1); | ||
} | ||
|
||
// Check if the character counts are the same | ||
return count1.keySet().equals(count2.keySet()) && count1.values().equals(count2.values()); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.