-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringAllUniqueChar.java
More file actions
78 lines (75 loc) · 3.22 KB
/
StringAllUniqueChar.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
package stringalluniquechar;
import java.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
*/
public class StringAllUniqueChar {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Test case for array analysis
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the string you want analyzed with "
+ "arrayAnalysis");
String input = keyboard.nextLine();
System.out.println("You enetered the following string : "+input);
boolean duplicatesNotFound = arrayAnalysis(input);
System.out.println("Using arrayAnalysis determined the following: ");
if(!duplicatesNotFound) System.out.println("Duplicates were found");
else System.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);
boolean duplicatesNotFound2 = arrayAnalysis(input);
System.out.println("Using comparativeAnalysis determined "
+ "the following: ");
if(!duplicatesNotFound2) System.out.println("Duplicates were found");
else System.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
*/
public static boolean arrayAnalysis(String in){
boolean duplicateNotFound = true;
int[] array = new int[127];
for(int j = 0; j < in.length(); j ++){ //for each char in string
char current = in.charAt(j);
array[current]++;//
if ( array[current] > 1){
duplicateNotFound = false;
break;
}
}
return duplicateNotFound;
}
/*
* 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
*/
public static boolean comparativeAnalysis(String in){
boolean duplicateNotFound = true;
for (int i = 0; i < in.length(); i++){ //for each char in string
char current = in.charAt(i);
//check all other chars for duplicates
for(int j = 0; j < in.length(); j++){
if(j != i){ //do not compare current to itself
char compare = in.charAt(j);
if (compare == current) return false;
}
}
}
return duplicateNotFound;
}
}