Skip to content

Commit

Permalink
Added codes for 8 March
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Mar 8, 2024
1 parent 8766d4e commit 835b6f1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
62 changes: 62 additions & 0 deletions GeeksForGeeks/March/08-3-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//{ Driver Code Starts
//Initial Template for Java

import java.io.*;
import java.util.*;

class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {

String input[] = read.readLine().split(" ");
String a = input[0];

Solution ob = new Solution();
if(ob.sameFreq(a)){
System.out.println(1);
}
else{
System.out.println(0);
}
}
}
}
// } Driver Code Ends


//User function Template for Java

class Solution
{
boolean sameFreq(String s)
{
// code here
int arr[]=new int[26];

for(int i=0;i<s.length();i++)
{
arr[s.charAt(i)-97]++;
}

int ans=-1,c=0;
for(int i : arr)
{
if(i!=0)
{
if(ans==-1)
ans=i;

else if (Math.abs(ans-i)>1)
return false;

else if(Math.abs(ans-i)==1 && ++c==2)
return false;
}
}

return true;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/08-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/March/08-3-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)
20 changes: 20 additions & 0 deletions LeetCode/March/08-3-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution
{
public int maxFrequencyElements(int[] nums)
{
int kMax = 100;
int ans = 0;
int[] count = new int[kMax + 1];

for (int num : nums)
++count[num];

int maxFreq = Arrays.stream(count).max().getAsInt();

for (int freq : count)
if (freq == maxFreq)
ans += maxFreq;

return ans;
}
}

0 comments on commit 835b6f1

Please sign in to comment.