Skip to content

Commit

Permalink
Added for 13 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 13, 2024
1 parent c8cfbb2 commit a57d32c
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
111 changes: 111 additions & 0 deletions GeeksForGeeks/13-1-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//{ Driver Code Starts
//Initial Template for Java

import java.util.*;
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
class insertion
{
Node head;
Node tail;
public void addToTheLast(Node node)
{
if (head == null)
{
head = node;
tail = node;
}
else
{
tail.next = node;
tail = node;
}
}
void printList(Node head)
{
Node temp = head;
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
/* Drier program to test above functions */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t>0)
{
int n = sc.nextInt();
insertion llist = new insertion();
int a1=sc.nextInt();
Node head= new Node(a1);
llist.addToTheLast(head);
for (int i = 1; i < n; i++)
{
int a = sc.nextInt();
llist.addToTheLast(new Node(a));
}

Solution ob = new Solution();
head = ob.insertionSort(llist.head);
llist.printList(head);

t--;
}
}}
// } Driver Code Ends


//User function Template for Java

/*class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
*/
class Solution
{
static Node sorted;

public static Node insertionSort(Node headref)
{
sorted = null;
Node current = headref;
while (current != null)
{
Node next = current.next;
sortedInsert(current);
current = next;
}
headref = sorted;
return headref;
}

static void sortedInsert(Node newnode)
{
if (sorted == null || sorted.data >= newnode.data)
{
newnode.next = sorted;
sorted = newnode;
}
else
{
Node current = sorted;
while (current.next != null && current.next.data < newnode.data)
{
current = current.next;
}
newnode.next = current.next;
current.next = newnode;
}
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/13-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^2)
Space complexity - O(1)
3 changes: 3 additions & 0 deletions LeetCode/13-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Time complexity - O(len(s+t))
Space complexity - O(26) = O(1)

15 changes: 15 additions & 0 deletions LeetCode/13-1-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution
{
public int minSteps(String s, String t)
{
int[] count = new int[26];

for (final char c : s.toCharArray())
++count[c - 'a'];

for (final char c : t.toCharArray())
--count[c - 'a'];

return Arrays.stream(count).map(Math::abs).sum() / 2;
}
}

0 comments on commit a57d32c

Please sign in to comment.