From f0c12e0fe72594e2a9b9ff09a434fd3a98e85b80 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Tue, 30 Apr 2024 08:42:55 +0530 Subject: [PATCH] Added codes for 30 April --- GeeksForGeeks/April/30-4-24/GFG.java | 148 ++++++++++++++++++++++++++ GeeksForGeeks/April/30-4-24/README.md | 2 + LeetCode/April/30-4-24/README.md | 2 + LeetCode/April/30-4-24/Solution.java | 23 ++++ 4 files changed, 175 insertions(+) create mode 100644 GeeksForGeeks/April/30-4-24/GFG.java create mode 100644 GeeksForGeeks/April/30-4-24/README.md create mode 100644 LeetCode/April/30-4-24/README.md create mode 100644 LeetCode/April/30-4-24/Solution.java diff --git a/GeeksForGeeks/April/30-4-24/GFG.java b/GeeksForGeeks/April/30-4-24/GFG.java new file mode 100644 index 0000000..53bbeeb --- /dev/null +++ b/GeeksForGeeks/April/30-4-24/GFG.java @@ -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 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; + } +} diff --git a/GeeksForGeeks/April/30-4-24/README.md b/GeeksForGeeks/April/30-4-24/README.md new file mode 100644 index 0000000..e95b48c --- /dev/null +++ b/GeeksForGeeks/April/30-4-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n+m) +Space complexity - O(n) diff --git a/LeetCode/April/30-4-24/README.md b/LeetCode/April/30-4-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/April/30-4-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/April/30-4-24/Solution.java b/LeetCode/April/30-4-24/Solution.java new file mode 100644 index 0000000..6732b4e --- /dev/null +++ b/LeetCode/April/30-4-24/Solution.java @@ -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; + } +}