-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
481a4f4
commit f0c12e0
Showing
4 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
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,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; | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(n+m) | ||
Space complexity - O(n) |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
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,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; | ||
} | ||
} |