-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathQuestion_3.java
50 lines (44 loc) · 1.38 KB
/
Question_3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Question 3
__________
you are given a 0-indexed string word, consisting of lowercase English letters. You need to select one index and remove the letter at that index from word so that the frequency of every letter present in word is equal.
Return true if it is possible to remove one letter so that the frequency of all letters in word are equal, and false otherwise.
Note:
The frequency of a letter x is the number of times it occurs in the string.
You must remove exactly one letter and cannot chose to do nothing.
Input: word = "abcc"
Output: true
Explanation: Select index 3 and delete it: word becomes "abc" and each character has a frequency of 1.
*/
import java.util.*;
public class CheckString {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int flag=0;
String s=sc.nextLine();
StringBuilder sb=new StringBuilder(s);
for(int i=0;i<sb.length()-1;i++) { //to remove duplicate element
for(int j=i+1;j<sb.length();j++) {
if(sb.charAt(i)==sb.charAt(j)) {
sb.deleteCharAt(j);
i=sb.length();
break;
}
}
}
for(int i=0;i<sb.length()-1;i++) { //to check if still duplicate element exists
for(int j=i+1;j<sb.length();j++) {
if(sb.charAt(i)==sb.charAt(j)) {
flag=1;
i=sb.length();
break;
}
}
}
if(flag==1)
System.out.println("False");
else
System.out.println("True");
sc.close();
}
}