-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckDistinctCharacters.java
More file actions
47 lines (30 loc) · 1.28 KB
/
Copy pathCheckDistinctCharacters.java
File metadata and controls
47 lines (30 loc) · 1.28 KB
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
import java.util.*;
public class CheckDistinctCharacters {
/**
* @param str: a string
* @return: a boolean
*/
public static boolean is_Unique_str(String str) {
// Convert the input string to a character array
char[] chars = str.toCharArray()
// Sort the character array in lexicographical order
Arrays.sort(chars);
// Check for repeated characters in the sorted array
for (int i = 1; i < chars.length; ++i) {
if (chars[i] == chars[i-1]) {
return false;
}
}
// If no repeated characters are found, the string is considered to have all unique characters
return true;
}
public static void main(String[] args) {
// Test case: Check if the string "xyyz" has all unique characters
// Note: You can change the value of the 'str' variable for different input strings.
String str = "xyyz";
// Print the original string
System.out.println("Original String : " + str);
// Check if the string has all unique characters and print the result
System.out.println("String has all unique characters: " + is_Unique_str(str));
}
}