Skip to content

Commit

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

import java.util.*;
class ModifyQueue {
public static void main(String[] args) {

// Taking input using class Scanner
Scanner sc = new Scanner(System.in);

// Taking total number of testcases
int t = sc.nextInt();

while (t-- > 0) {
// Taking count of total number of elements
int n = sc.nextInt();

// Taking count of total elements
// that need to be reversed
int k = sc.nextInt();

// Creating a Queue
Queue<Integer> q = new LinkedList<>();

// adding all the elements to the Queue
while (n-- > 0) {
q.add((int)sc.nextInt());
}

// Creating an object of class GfG
GfG g = new GfG();

// calling modifyQueue of class GfG
// and passing Queue and k as arguments
// and storing the reuslt in a new Queue
Queue<Integer> ans = g.modifyQueue(q, k);

// Printing all the elements from the
// new Queue and polling them out
while (!ans.isEmpty()) {
int a = ans.peek();
ans.poll();
System.out.print(a + " ");
}
System.out.println();
}
}
}

// } Driver Code Ends


// User function Template for Java

class GfG {
// Function to reverse first k elements of a queue.
public Queue<Integer> modifyQueue(Queue<Integer> q, int k)
{
// add code here.
Deque<Integer> d = new ArrayDeque<>();

for (int i = 0; i < k; i++)
d.push(q.poll());

while (!d.isEmpty())
q.add(d.pop());

for (int i = 0; i < q.size() - k; i++)
q.add(q.poll());

return q;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/12-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(k)
2 changes: 2 additions & 0 deletions LeetCode/12-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
16 changes: 16 additions & 0 deletions LeetCode/12-1-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution
{
public boolean halvesAreAlike(String s)
{
final String a = s.substring(0, s.length() / 2);
final String b = s.substring(s.length() / 2);
final int aVowelsCount = (int) a.chars().filter(c -> isVowel((char) c)).count();
final int bVowelsCount = (int) b.chars().filter(c -> isVowel((char) c)).count();
return aVowelsCount == bVowelsCount;
}

private boolean isVowel(char c)
{
return "aeiouAEIOU".indexOf(c) != -1;
}
}

0 comments on commit c8cfbb2

Please sign in to comment.