diff --git a/Algorithms/Neeraj/dataStructures/arraysAndString/CandiesDam.java b/Algorithms/Neeraj/dataStructures/arraysAndString/CandiesDam.java new file mode 100644 index 00000000..69dad3d4 --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/arraysAndString/CandiesDam.java @@ -0,0 +1,45 @@ +/* +Nehal wants to make a special space for candies on his bookshelf.Currently, it has N books of different heights and unit width. +Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. +The task is to find out the area between 2 books that can hold the maximum candies without changing the original position of selected books. +Example: +INPUT: N =3 + height[]={1,3,4} +OUTPUT:1 +*/ + +import java.io.*; +import java.util.*; + +class Solution +{ + static int maxCandy(int height[], int n) + { + int max = 0; + for (int i = 0; i < n - 1; i++) { + for (int j = i + 1; j < n; j++) { + int current = (Math.min(height[i],height[j])* (j - i - 1)); + max = Math.max(max, current); + } + } + return max; + } +} + +// Driver Code +class CandiesDam{ + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int height[] = new int[n]; + for (int i = 0; i < n; ++i) + { + height[i] = sc.nextInt(); + } + + Solution ob = new Solution(); + System.out.println(ob.maxCandy(height,n)); + } +} + diff --git a/Algorithms/Neeraj/dataStructures/arraysAndString/KPrefix.java b/Algorithms/Neeraj/dataStructures/arraysAndString/KPrefix.java new file mode 100644 index 00000000..5bd92fbb --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/arraysAndString/KPrefix.java @@ -0,0 +1,43 @@ +/* +Given an array A of size N.Find the Kth prefix of the array. +Kth prefix is defined as the prefix sum of (K-1)th prefix of an array. +Example 1: +Input: N=4 K=2 + Array= {1,1,1,1} +Output: 1 3 6 10 + +Example 2: +Input: N=1 K=100 + Array= {1} +Output: 1 +*/ + +import java.util.*; +import java.lang.*; +import java.io.*; + +class KPrefix { + public static void main (String[] args) { + //code + Scanner sc= new Scanner(System.in); + long M= 1000000007; //since the values can become pretty large + int n,k; + + n=sc.nextInt(); + k=sc.nextInt(); + int a[]=new int[n]; + for(int i=0;i=0;j--){ + for(int z=j-1;z>=0;z--){ + a[j]+=a[z]; + } + } + } + for(int i=0;i res = new Solution().possibleWords(arr, n); + for (String i : res) System.out.print (i + " "); + System.out.println(); + } +} +class Solution +{ + + static ArrayList possibleWords(int a[], int N) + { + String code[]={"\0","\0","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; + String s=""; + for(int i=0;i helper(String s,String[] code) + { + if(s.length()==0) + { + ArrayList res=new ArrayList(); + res.add(""); + return res; + } + char ch=s.charAt(0); + ArrayList res1=helper(s.substring(1),code); + ArrayList res3=new ArrayList(); + String str1=code[ch]; + for(int i=0;iarr[i] ) { + sum += Math.min(lmax[i], rmax[i]) - arr[i]; + } + } + return sum; + } +} + + diff --git a/Algorithms/Neeraj/dataStructures/arraysAndString/TripletSum.java b/Algorithms/Neeraj/dataStructures/arraysAndString/TripletSum.java new file mode 100644 index 00000000..8c6fa347 --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/arraysAndString/TripletSum.java @@ -0,0 +1,63 @@ +/* +Given an array arr of size n and an integer X. +Find if there's a triplet in the array which sums up to the given integer X. +Example: +INPUT: n = 6, X = 13 + arr[] = [1 4 45 6 10 8] +OUTPUT: 1 +*/ + +import java.util.*; +import java.io.*; +import java.lang.*; + +class TripletSum +{ + public static void main (String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + String inputLine[] = br.readLine().trim().split(" "); + int n = Integer.parseInt(inputLine[0]); + int X = Integer.parseInt(inputLine[1]); + int A[] = new int[n]; + inputLine = br.readLine().trim().split(" "); + for(int i=0; iarr[j]){ + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + } + for(int i=0;i sum){ + high--; + } + else{ + low++; + } + } + } + return false; + } +} diff --git a/Algorithms/Neeraj/dataStructures/arraysAndString/XTotalShapes.java b/Algorithms/Neeraj/dataStructures/arraysAndString/XTotalShapes.java new file mode 100644 index 00000000..3c835509 --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/arraysAndString/XTotalShapes.java @@ -0,0 +1,70 @@ +/* +Given a grid of n*m consisting of O's and X's. The task is to find the number of 'X' total shapes. +***'X' shape consists of one or more adjacent X's (diagonals not included). +Example: +INPUT: n=3 m=3 + grid = {{X,O,X},{O,X,O},{X,X,X}} +OUTPUT: 3 +*/ + +import java.util.*; +import java.lang.*; +import java.io.*; +//Driver code +class XTotalShapes +{ + public static void main(String[] args) throws IOException + { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().trim().split(" "); + int n = Integer.parseInt(s[0]); + int m = Integer.parseInt(s[1]); + char[][] grid = new char[n][m]; + for(int i = 0; i < n; i++){ + String S = br.readLine().trim(); + for(int j = 0; j < m; j++){ + grid[i][j] = S.charAt(j); + } + } + Solution obj = new Solution(); + int ans = obj.xShape(grid); + System.out.println(ans); + } +} + +class Solution +{ + //Function to find the number of 'X' total shapes. + public boolean val(char[][] grid,boolean[][] vis,int r,int c) + { + return (r>=0&&r=0&&c queue = new LinkedList<>(); + + queue.add(root); + // Starting from the second element + + int i = 1; + while(queue.size()>0 && i < ip.length) { + + // Get and remove the front of the queue + Node currNode = queue.peek(); + queue.remove(); + + // Get the current node's value from the string + String currVal = ip[i]; + + // If the left child is not null + if(!currVal.equals("N")) { + + // Create the left child for the current node + currNode.left = new Node(Integer.parseInt(currVal)); + // Push it to the queue + queue.add(currNode.left); + } + + // For the right child + i++; + if(i >= ip.length) + break; + + currVal = ip[i]; + + // If the right child is not null + if(!currVal.equals("N")) { + + // Create the right child for the current node + currNode.right = new Node(Integer.parseInt(currVal)); + + // Push it to the queue + queue.add(currNode.right); + } + i++; + } + + return root; + } + static void printInorder(Node root){ + if(root == null) + return; + + printInorder(root.left); + System.out.print(root.data+" "); + + printInorder(root.right); + } + + public static void main (String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String s = br.readLine(); + Node root = buildTree(s); + + Solution ob = new Solution(); + System.out.println(ob.LISS(root)); + } +} + +class Solution { + public int LISS(Node node){ + if(node==null){ + return 0; + } + int se = LISS(node.left)+LISS(node.right); + + int si = 1; + if(node.left!=null){ + si+=LISS(node.left.left)+LISS(node.left.right); + } + if(node.right!=null){ + si+=LISS(node.right.left)+LISS(node.right.right); + } + return Math.max(se,si); + } +} diff --git a/Algorithms/Neeraj/dataStructures/binarytree/PreOrderToPostOrder.java b/Algorithms/Neeraj/dataStructures/binarytree/PreOrderToPostOrder.java new file mode 100644 index 00000000..5486458c --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/binarytree/PreOrderToPostOrder.java @@ -0,0 +1,83 @@ +/* +Given an array arr[] of N nodes representing preorder traversal of BST. The task is to print its postorder traversal. +Example: +INPUT: N = 5 + arr[] = 40 30 35 80 100 +OUTPUT: 35 30 100 80 40 +*/ +import java.util.*; +import java.io.*; + +class Node { + int data; + Node left, right; + Node(int d) { + data = d; + left = right = null; + } +} + +class PreOrderToPostOrder { + + public static void main(String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] inputline = br.readLine().trim().split(" "); + int n = Integer.parseInt(inputline[0]); + inputline = br.readLine().trim().split(" "); + int[] arr = new int[n]; + for(int i=0; iend) + return null; + Node root=new Node(pre[start]); + int i; + for(i=start+1;i<=end;i++) + { + if(pre[i]>root.data) + break; + } + root.left=constructTreeUtil(pre,start+1,i-1); + root.right=constructTreeUtil(pre,i,end); + return root; +} + +public static Node constructTree(int pre[], int size) { + return constructTreeUtil(pre,0,size-1); +} + + +public static void printInorder(Node node) { + if (node == null) { + return; + } + printInorder(node.left); + System.out.print(node.data + " "); + printInorder(node.right); + } + +public static void printPostorder(Node node) { + if (node == null) { + return; + } + printPostorder(node.left); + printPostorder(node.right); + System.out.print(node.data + " "); + } + +public static void printPreorder(Node node) { + if (node == null) { + return; + } + System.out.print(node.data + " "); + printPreorder(node.left); + printPreorder(node.right); + } +} \ No newline at end of file diff --git a/Algorithms/Neeraj/dataStructures/binarytree/ValentineSum.java b/Algorithms/Neeraj/dataStructures/binarytree/ValentineSum.java new file mode 100644 index 00000000..0f4f8162 --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/binarytree/ValentineSum.java @@ -0,0 +1,126 @@ +/* +Cupid wants to strike maximum houses in Geek Land on Valentine's Day. The houses in Geek Land are arranged in the form of a binary tree. Cupid is standing at target node initially. +Find the sum of all nodes within a maximum distance k from target node. The target node should be included in the sum. +Example: +INPUT: String: 1 2 3 N N 4 6 N 5 N N 7 N + target=9 K=1 +OUTPUT:22 +*/ + +import java.util.LinkedList; +import java.util.Queue; +import java.io.*; +import java.util.*; + +class Node{ + int data; + Node left; + Node right; + Node(int data){ + this.data = data; + left=null; + right=null; + } +} + +class ValentineSum { + + static Node buildTree(String str){ + + if(str.length()==0 || str.charAt(0)=='N'){ + return null; + } + + String ip[] = str.split(" "); + Node root = new Node(Integer.parseInt(ip[0])); + + Queue queue = new LinkedList<>(); + + queue.add(root); + + int i = 1; + while(queue.size()>0 && i < ip.length) { + Node currNode = queue.peek(); + queue.remove(); + + String currVal = ip[i]; + if(!currVal.equals("N")) { + currNode.left = new Node(Integer.parseInt(currVal)); + queue.add(currNode.left); + } + + i++; + if(i >= ip.length) + break; + + currVal = ip[i]; + + if(!currVal.equals("N")) { + currNode.right = new Node(Integer.parseInt(currVal)); + queue.add(currNode.right); + } + i++; + } + + return root; + } + + public static void main (String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String line = br.readLine().trim(); + Node root = buildTree(line); + + line = br.readLine().trim(); + String target_k[] = line.split(" "); + int target = Integer.parseInt(target_k[0]); + int k = Integer.parseInt(target_k[1]); + + Solution x = new Solution(); + System.out.println( x.sum_at_distK(root, target, k) ); + } +} + +class Solution{ + static int sum; + static void add_subtree(Node n, int dist) + { + if ( (n==null) || (dist<0) ) return; + sum += n.data; + add_subtree(n.left, dist-1); + add_subtree(n.right, dist-1); + } + + static int traverse(Node n, int target, int k) + { + if (n==null) return -1; + if (n.data==target) + { + add_subtree(n, k); + return k-1; + } + + int dist = traverse(n.left, target, k); + if (-1 < dist) + { + sum += n.data; + add_subtree(n.right, dist-1); + return dist-1; + } + + dist = traverse(n.right, target, k); + if (-1 < dist) + { + sum += n.data; + add_subtree(n.left, dist-1); + return dist-1; + } + return -1; + } + static int sum_at_distK(Node root, int target, int k) + { + sum = 0; + traverse(root, target, k); + return sum; + } + +} \ No newline at end of file diff --git a/Algorithms/Neeraj/dataStructures/bitwise/BitDifference.java b/Algorithms/Neeraj/dataStructures/bitwise/BitDifference.java new file mode 100644 index 00000000..d3a747fd --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/bitwise/BitDifference.java @@ -0,0 +1,43 @@ +/* +Given an integer array of size N . You have to find sum of bit differences in all pairs that can be formed from array elements. +Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. +Example: +INPUT: N = 2, arr[] = {1, 2} +OUTPUT: 4 +*/ + +import java.io.*; +import java.util.*; + +class BitDifference +{ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int arr[] = new int[n]; + for(int i = 0 ;i> graph = new HashMap<>(); + + int[] in = new int[26]; + int[] out = new int[26]; + + for(String str:arr){ + + int src = str.charAt(0)-'a'; + int dest = str.charAt(str.length()-1)-'a'; + + hasAlpha[src] = hasAlpha[dest] = true; + + in[dest]++; + out[src]++; + + if(!graph.containsKey(src)) graph.put(src,new ArrayList<>()); + graph.get(src).add(dest); + + } + + for(int i=0;i<26;i++){ + if(in[i]!=out[i]) return 0; + } + + boolean[] visited = new boolean[26]; + dfs(arr[0].charAt(0)-'a',graph,hasAlpha,visited); + + for(int i=0;i<26;i++){ + if(hasAlpha[i] && !visited[i]) return 0; + } + return 1; + } + + private static void dfs(int node, HashMap> graph, boolean[] hasAlpha, boolean[] visited){ + + visited[node] = true; + + for (Integer nbr : graph.get(node)) { + if(hasAlpha[nbr] && !visited[nbr]) dfs(nbr,graph,hasAlpha,visited); + } + + } +} diff --git a/Algorithms/Neeraj/dataStructures/hashtable/AnagramTogether.java b/Algorithms/Neeraj/dataStructures/hashtable/AnagramTogether.java new file mode 100644 index 00000000..da513716 --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/hashtable/AnagramTogether.java @@ -0,0 +1,69 @@ +/* +Given an array of strings, return all groups of strings that are anagrams. +The groups must be created in order of their appearance in the original array. +Example: +INPUT: N=5 + words[]={act,dog,cat,tac,god} +OUTPUT: act,cat,tac + dog,god +*/ + +//Initial Template for Java + +/*package whatever //do not write package name here */ + +import java.io.*; +import java.util.*; + +class AnagramTogether { + public static void main (String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int n= Integer.parseInt(br.readLine().trim()); + String x = br.readLine().trim(); + String string_list[] = x.split(" ",n); + + Solution ob = new Solution(); + + List > ans = ob.Anagrams(string_list); + + Collections.sort(ans, new Comparator>(){ + public int compare(List l1, List l2) { + String s1 = l1.get(0); + String s2 = l2.get(0); + + return s1.compareTo(s2); + } + }); + + for(int i=0;i> Anagrams(String[] string_list) { + List> res = new ArrayList<>(); + HashMap> hm = new HashMap<>(); + for(String s : string_list){ + char c[] = s.toCharArray(); + Arrays.sort(c); + String str = new String(c); + if(!hm.containsKey(str)){ + hm.put(str,new ArrayList<>()); + } + hm.get(str).add(s); + + } + res.addAll(hm.values()); + return res; + } +} + + diff --git a/Algorithms/Neeraj/dataStructures/hashtable/EqualCountPairs.java b/Algorithms/Neeraj/dataStructures/hashtable/EqualCountPairs.java new file mode 100644 index 00000000..ccdb5a33 --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/hashtable/EqualCountPairs.java @@ -0,0 +1,48 @@ +/* +For a given array A, find the number of pairs(i,j) such that +imp=new HashMap<>(); + for(int i=0;ie:mp.entrySet()) + { + long val=e.getValue(); + ans+=(val*(val-1))/2; + } + return ans; + } +} + +// Driver Code + +class EqualCountPairs +{ + public static void main(String args[])throws IOException + { + BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); + + int N=Integer.parseInt(read.readLine()); + String input[] = read.readLine().trim().split("\\s+"); + int[]A=new int[N]; + for(int i = 0; i < N; i++) + A[i]=Integer.parseInt(input[i]); + Solution ob = new Solution(); + System.out.println(ob.equalCount(N, A)); + } +} \ No newline at end of file diff --git a/Algorithms/Neeraj/dataStructures/hashtable/UniqueList.java b/Algorithms/Neeraj/dataStructures/hashtable/UniqueList.java new file mode 100644 index 00000000..08e9ae59 --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/hashtable/UniqueList.java @@ -0,0 +1,45 @@ +/* +Given a list of N strings. Find out if the list of strings is valid or not. +A list is said to be valid when number of duplicate strings do not exceed the number +of unique strings. Strings are not case-sensitive. +Example 1: +INPUT: list={"cd","cdd","cd","cdd"} +OUTPUT: Yes + +Example 2: +INPUT: list={"pqr","PQr","Pqr"} +OUTPUT: No +*/ + +import java.util.*; +import java.io.*; + +class Solution{ + public String uniqueList(ArrayListlist){ + HashSet h= new HashSet(); + for(String s:list) + { + h.add(s.toLowerCase()); + } + + int duplicates = list.size() - h.size(); + if(2*h.size()list=new ArrayList<>(); + for(int i = 0; i < input.length; i++) + list.add(input[i]); + Solution ob = new Solution(); + System.out.println(ob.uniqueList(list)); + } +} \ No newline at end of file diff --git a/Algorithms/Neeraj/dataStructures/heapAndSort/kClosestNeighbour.java b/Algorithms/Neeraj/dataStructures/heapAndSort/kClosestNeighbour.java new file mode 100644 index 00000000..c2c82f56 --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/heapAndSort/kClosestNeighbour.java @@ -0,0 +1,102 @@ +/* +Lucy lives in house number X. She has a list of N house numbers in the society. Distance between houses can be determined by studying the difference between house numbers. Help her find out K closest neighbors. +**If two houses are equidistant and Lucy has to pick only one, the house with the smaller house number is given preference. +Example: +INPUT:N = 5, X = 0, K = 4 + a[] = {-21, 21, 4, -12,20} +OUTPUT: -21 -12 4 20 +*/ + +import java.io.*; +import java.util.*; +import java.lang.*; + +class Info +{ + int distance; + int houseno; + Info(int x,int y) + { + distance = x; + houseno = y; + } +} + +class Compare implements Comparator +{ + public int compare (Info p1,Info p2) + { + if (p1.distance == p2.distance) + { + if (p1.houseno < p2.houseno) + return +1; + if (p1.houseno > p2.houseno) + return -1; + return 0; + } + else + { + if (p1.distance < p2.distance) + return +1; + if (p1.distance > p2.distance) + return -1; + return 0; + } + } +} +class Solution +{ + public ArrayList Kclosest(int arr[], int n, int x, int k) + { + ArrayList result= new ArrayList(); + PriorityQueue pq = new PriorityQueue(k, new Compare()); + + for (int i = 0; i < k; i++) + { + Info obj = new Info(Math.abs(arr[i] - x) , arr[i]); + pq.add(obj); + } + + for (int i = k; i < n; i++) + { + int diff = Math.abs(arr[i] - x); + if (pq.peek().distance < diff) + continue; + + if (diff == pq.peek().distance && pq.peek().houseno < arr[i]) + continue; + + pq.remove(); + Info obj = new Info(Math.abs(arr[i] - x) , arr[i]); + pq.add(obj); + } + while (0 < pq.size()) + { + result.add(pq.peek().houseno); + pq.remove(); + } + Collections.sort(result); + return result; + } +} + +// Driver Code +class kClosestNeighbour{ + public static void main(String args[]) throws IOException { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int x = sc.nextInt(); + int k = sc.nextInt(); + int arr[] = new int[n]; + for(int i=0; i ans = ob.Kclosest(arr,n,x,k); + + for (int i=0; i ans = new ArrayList(); + ans = obj.rotation(N); + for(int i: ans) + System.out.print(i + " "); + System.out.println(); + } +} + +class Solution{ + + ArrayList rotation(int n){ + int[] res = new int[n]; + + int j = 0; + for(int i = 1; i <= n; i++){ + int count =- 1; + while(true){ + if(res[j%n] == 0) count++; + if(count == i){ + res[j%n] = i; + break; + } + j++; + } + } + + ArrayList ans = new ArrayList<>(); + for(int i = 0; i < n; i++){ + ans.add(res[i]); + } + + return ans; + } +} \ No newline at end of file diff --git a/Algorithms/Neeraj/dataStructures/queueAndStack/GetMinimumElement.java b/Algorithms/Neeraj/dataStructures/queueAndStack/GetMinimumElement.java new file mode 100644 index 00000000..fbcd63da --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/queueAndStack/GetMinimumElement.java @@ -0,0 +1,89 @@ +/* +Given N elements and the task is to Implement a Stack in which you can get minimum element in O(1) time. +First line of input denotes Q. +A Query Q may be of 3 Types: + 1. 1 x (a query of this type means pushing 'x' into the stack) + 2. 2 (a query of this type means to pop element from stack and print the poped element) + 3. 3 (a query of this type means to print the minimum element from the stack) +The second line contains Q queries seperated by space. +Example: +INPUT: 6 + 1 2 1 3 2 3 1 1 3 +OUTPUT: 3 2 1 +*/ + + +import java.util.*; + +class GetMinimumElement +{ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int q = sc.nextInt(); + Solution sol = new Solution(); + while(q>0) + { + int qt = sc.nextInt(); + if(qt == 1) + { + int att = sc.nextInt(); + sol.push(att); + } + else if(qt == 2) + { + System.out.print(sol.pop()+" "); + } + else if(qt == 3) + { + System.out.print(sol.getMin()+" "); + } + q--; + } + System.out.println(); + } +} + +class Solution +{ + int minEle; + Stack s=new Stack<>(); + + int getMin() + { + if(s.isEmpty()) return -1; + return minEle; + } + + int pop() + { + if(!s.isEmpty()){ + int temp = s.pop(); + if(temp>=minEle){ + return temp; + } else{ + int tem = minEle; + minEle = minEle-temp; + return tem; + } + } + return -1; + } + + void push(int x) + { + if(s.isEmpty()){ + s.push(x); + minEle=x; + }else{ + if(x>=minEle){ + s.push(x); + }else{ + int temp = x-minEle; + s.push(temp); + minEle=x; + } + } + } +} + diff --git a/Algorithms/Neeraj/dataStructures/queueAndStack/RestrictiveCandyCrush.java b/Algorithms/Neeraj/dataStructures/queueAndStack/RestrictiveCandyCrush.java new file mode 100644 index 00000000..c019cb85 --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/queueAndStack/RestrictiveCandyCrush.java @@ -0,0 +1,102 @@ +/* +Given a string s and an integer k, the task is to reduce the string by applying the following operation: +Choose a group of k consecutive identical characters and remove them. +The operation can be performed any number of times until it is no longer possible. +Example: +INPUT: k=2 + s="geeksforgeeks" +OUTPUT: gksforgks +*/ + +import java.util.*; +import java.math.*; +import java.io.*; + +class FastReader{ + BufferedReader br; + StringTokenizer st; + + public FastReader(){ + br = new BufferedReader(new InputStreamReader(System.in)); + } + + String next(){ + while (st == null || !st.hasMoreElements()){ + try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } + } + return st.nextToken(); + } + + String nextLine(){ + String str = ""; + try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } + return str; + } +} + +class RestrictiveCandyCrush +{ + public static void main(String args[]) + { + FastReader sc = new FastReader(); + PrintWriter out = new PrintWriter(System.out); + int k = Integer.parseInt(sc.next()); + String s = sc.next(); + Solution T = new Solution(); + out.println(T.reduced_String(k, s)); + out.flush(); + } +} + +class Solution +{ + static class Node{ + char value; + int count; + public Node(char value,int count){ + this.value=value; + this.count=count; + } +} + + public static String reduced_String(int k, String s) + { + if(k==1){ + return ""; + } + + Stack stack=new Stack<>(); + int n=s.length(); + for(int i=0;i0){ + sb.insert(0,ch); + } + } + + return sb.toString(); + + } +} \ No newline at end of file diff --git a/Algorithms/Neeraj/dataStructures/string/RepeatedStringMatch.java b/Algorithms/Neeraj/dataStructures/string/RepeatedStringMatch.java new file mode 100644 index 00000000..f0628dcf --- /dev/null +++ b/Algorithms/Neeraj/dataStructures/string/RepeatedStringMatch.java @@ -0,0 +1,45 @@ +/* +Given two strings A and B, find the minimum number of times A has to be repeated such that B becomes a substring of the repeated A. +If B cannot be a substring of A no matter how many times it is repeated, return -1. +Example: +INPUT: A="abcd" B="cdabcdab" +OUTPUT: 3 +*/ + + +import java.io.*; +import java.util.*; + +class RepeatedStringMatch{ + public static void main(String args[]) throws IOException { + Scanner sc = new Scanner(System.in); + String A = sc.nextLine(); + String B = sc.nextLine(); + Solution ob = new Solution(); + System.out.println(ob.repeatedStringMatch(A,B)); + } +} + +class Solution +{ + static int repeatedStringMatch(String A, String B) + { + // Your code goes here + String o = A; + int a= B.length()/A.length(); + int c=1; + + for(int i=0; i