-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8766d4e
commit 835b6f1
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |