- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringAllUniqueChar.java
More file actions
Latest commit
78 lines (75 loc) · 3.22 KB
/
Copy pathStringAllUniqueChar.java
File metadata and controls
78 lines (75 loc) · 3.22 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
packagestringalluniquechar;
importjava.util.Scanner;
/**
*
* @author JOSEPH CONQUEST
* String All Unique Char reads a String from command prompt
* And determines if the string contains any duplicate chars
* Printing out the result to the command prompt
*/
publicclassStringAllUniqueChar {
/**
* @param args the command line arguments
*/
publicstaticvoidmain(String[] args) {
//Test case for array analysis
Scannerkeyboard = newScanner(System.in);
System.out.println("Please enter the string you want analyzed with "
+ "arrayAnalysis");
Stringinput = keyboard.nextLine();
System.out.println("You enetered the following string : "+input);
booleanduplicatesNotFound = arrayAnalysis(input);
System.out.println("Using arrayAnalysis determined the following: ");
if(!duplicatesNotFound) System.out.println("Duplicates were found");
elseSystem.out.println("Duplicates were not found");
//Test case for comparative analysis
System.out.println("Please enter the string you want analyzed with "
+ "arrayAnalysis");
input = keyboard.nextLine();
System.out.println("You enetered the following string : "+input);
booleanduplicatesNotFound2 = arrayAnalysis(input);
System.out.println("Using comparativeAnalysis determined "
+ "the following: ");
if(!duplicatesNotFound2) System.out.println("Duplicates were found");
elseSystem.out.println("Duplicates were not found");
}
/*
* Method takes in a string "in" for a parameter and anaylzes in for any
* duplicate chars. Method uses array of int to anaylze duplication,
* requiring extra overhead for execution but providing O(n) to analyze
* RETURN If a duplicate is found, the method returns false
*/
publicstaticbooleanarrayAnalysis(Stringin){
booleanduplicateNotFound = true;
int[] array = newint[127];
for(intj = 0; j < in.length(); j ++){ //for each char in string
charcurrent = in.charAt(j);
array[current]++;//
if ( array[current] > 1){
duplicateNotFound = false;
break;
}
}
returnduplicateNotFound;
}
/*
* Method takes in a string "in" for a parameter and anaylzes in for any
* duplicate chars. Method compares each char to every other char of in,
* requiring O(n^2) to analyze, but not requiring data structure overhead
* RETURN If a duplicate is found, the method returns false
*/
publicstaticbooleancomparativeAnalysis(Stringin){
booleanduplicateNotFound = true;
for (inti = 0; i < in.length(); i++){ //for each char in string
charcurrent = in.charAt(i);
//check all other chars for duplicates
for(intj = 0; j < in.length(); j++){
if(j != i){ //do not compare current to itself
charcompare = in.charAt(j);
if (compare == current) returnfalse;
}
}
}
returnduplicateNotFound;
}
}