Skip to content

Commit 6f0103b

Browse files
committed
Distinct Characters
1 parent 7b61274 commit 6f0103b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/easy/DistinctCharacters.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package easy;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* Have the function DistinctCharacters(str) take the str parameter being passed
8+
* and determine if it contains at least 10 distinct characters,
9+
* if so, then your program should return the string true,
10+
* otherwise it should return the string false.
11+
* ---
12+
* For example: if str is "abc123kkmmmm?" then your program should return the string false
13+
* because this string contains only 9 distinct characters:
14+
* a, b, c, 1, 2, 3, k, m. ? adds up to 9.
15+
*/
16+
public class DistinctCharacters {
17+
18+
/**
19+
* Distinct Characters function.
20+
*
21+
* @param str input string
22+
* @return "true" if a string contains at least 10 distinct characters.
23+
*/
24+
private static String distinctCharacters(String str) {
25+
Set<Character> table = new HashSet<>();
26+
for (int i = 0; i < str.length(); i++) {
27+
table.add(str.charAt(i));
28+
}
29+
return table.size() >= 10 ? "true" : "false";
30+
}
31+
32+
/**
33+
* Entry point.
34+
*
35+
* @param args command line arguments
36+
*/
37+
public static void main(String[] args) {
38+
var result1 = distinctCharacters("12334bbmma:=6");
39+
System.out.println(result1);
40+
var result2 = distinctCharacters("001122334455667788");
41+
System.out.println(result2);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)