Public Class Public Static Void: "Not Unique"
Public Class Public Static Void: "Not Unique"
java
1 public class UniqueChar {
2
3 public static void main(String[] args) {
4 // String str = "hey";
5 // System.out.println(isUnique(str));
6
7
8 // String[] words = {"abcde", "hello", "apple", "kite", "
padle"};
9 // for (String word:words) {
10 // System.out.println(word+": "+isUniqueArray(word));
11 // }
12
13 }
14
15 public static String isUnique(String str) {
16 for (int i=0; i<=str.length()‐1; i++){
17 for (int j=i+1; j<=str.length()‐1; j++){
18 if (str.charAt(i)==str.charAt(j)){
19 return "not unique";
20 }
21 }
22 }
23 return "is unique";
24 }
25
26 public static boolean isUniqueArray(String str){
27 if (str.length()>128){
28 return false;
29 }
30
31 for (int i=0; i<=str.length()‐1; i++){
32 for (int j=i+1; j<=str.length()‐1; j++){
33 if (str.charAt(i)==str.charAt(j)){
34 return false;
35 }
36 }
37 }
38 return true;
39 }
40
41
42 }
43
Page 1 of 1