diff --git a/GeeksForGeeks/12-1-24/GFG.java b/GeeksForGeeks/12-1-24/GFG.java new file mode 100644 index 0000000..dc90325 --- /dev/null +++ b/GeeksForGeeks/12-1-24/GFG.java @@ -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 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 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 modifyQueue(Queue q, int k) + { + // add code here. + Deque 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; + } +} diff --git a/GeeksForGeeks/12-1-24/README.md b/GeeksForGeeks/12-1-24/README.md new file mode 100644 index 0000000..392ea8e --- /dev/null +++ b/GeeksForGeeks/12-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(k) diff --git a/LeetCode/12-1-24/README.md b/LeetCode/12-1-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/LeetCode/12-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/12-1-24/Solution.java b/LeetCode/12-1-24/Solution.java new file mode 100644 index 0000000..12491f3 --- /dev/null +++ b/LeetCode/12-1-24/Solution.java @@ -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; + } +}