Skip to content

Commit

Permalink
Added codes for 30 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 30, 2024
1 parent 481a4f4 commit f0c12e0
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
148 changes: 148 additions & 0 deletions GeeksForGeeks/April/30-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//{ Driver Code Starts
// driver

import java.util.*;

class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}

class GfG{

static void printList(Node n){
while(n!=null){
System.out.print(n.data+" ");
n = n.next;
}
System.out.println();
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();

while (T-- > 0) {

int n = sc.nextInt();
int val = sc.nextInt();

Node num1 = new Node(val);
Node tail = num1;
for(int i=0; i<n-1; i++)
{
val = sc.nextInt();
tail.next = new Node(val);
tail = tail.next;
}

int m = sc.nextInt();
val = sc.nextInt();

Node num2 = new Node(val);
tail = num2;
for(int i=0; i<m-1; i++)
{
val = sc.nextInt();
tail.next = new Node(val);
tail = tail.next;
}

Solution g = new Solution();
Node res = g.addTwoLists(num1, num2);
printList(res);
}
}
}

// } Driver Code Ends


/* node for linked list
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
*/

class Solution
{
//Function to add two numbers represented by linked list.
static Node reverse(Node head)
{
Node prev = null;
Node current = head;
Node next = null;

while (current != null)
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}

return prev;
}

static Node addTwoLists(Node num1, Node num2)
{
// code here
// return head of sum list
num1 = reverse(num1);
num2 = reverse(num2);

Node sum = null;
int carry = 0;

while (num1 != null || num2 != null || carry > 0)
{
int newVal = carry;

if (num1 != null)
{
newVal += num1.data;
num1 = num1.next;
}
if (num2 != null)
{
newVal += num2.data;
num2 = num2.next;
}

carry = newVal / 10;

newVal = newVal % 10;

Node newNode = new Node(newVal);

newNode.next = sum;

sum = newNode;
}

while (sum != null && sum.data == 0)
{
Node temp = sum.next;
sum.next = null;
sum = temp;
}

if (sum == null)
return new Node(0);

return sum;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/30-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n+m)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/April/30-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
23 changes: 23 additions & 0 deletions LeetCode/April/30-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution
{
public long wonderfulSubstrings(String word)
{
long ans = 0;
int prefix = 0;
int[] count = new int[1024];
count[0] = 1;

for (char c : word.toCharArray())
{
prefix ^= 1 << c - 'a';
ans += count[prefix];

for (int i = 0; i < 10; ++i)
ans += count[prefix ^ 1 << i];

++count[prefix];
}

return ans;
}
}

0 comments on commit f0c12e0

Please sign in to comment.