-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuplicateString.java
83 lines (71 loc) · 3.26 KB
/
DuplicateString.java
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
79
80
81
82
83
import java.util.ArrayList;//unused--auto imported in other attempts
import java.util.Arrays;//unused--auto imported in other attemps
import java.util.HashSet;
import java.util.Set;
public class DuplicateString {
public static void main(String[] args) {
String firstString = "Abcd";//check string with just letters and no spaces with no duplicates
String secondString = "ABCda";//check string with upper/lower duplicate situation
String thirdString = "acd$%5";//check string with characters other than letters
String fourthString = "The Big";//check string with no duplicate letters but a space
String fifthString = "Do it be?";//check with not duplicate letters but two spaces
String sixthString = "There are duplicates here";
System.out.println(firstString + ": " + hasDuplicateAny(fifthString));//false in any method
System.out.println(secondString + ": " + hasDuplicateAny(secondString));//true
System.out.println(secondString + ": " + hasDuplicateNoCase(secondString));//false
//third and fourth strings come up false in any case
System.out.println(fifthString + ": " + hasDuplicateAny(fifthString));//true if counting spaces
System.out.println(fifthString + " Not including spaces: " + hasDuplicateNotSpaces(fifthString));//false if not counting spaces
System.out.println(sixthString + ": " + hasDuplicateNotSpaces(sixthString));//true in any case
}
/**
* This method takes in a string and checks to see if there are any duplicates including
* spaces or letters that are lower/upper case (i.e. A & a = duplicate)
* @param stringToCheck
* @return true if there is any duplicate, false if there is not
*/
public static boolean hasDuplicateAny(String stringToCheck) {
Set<Character> set = new HashSet<>();
String string = stringToCheck.toLowerCase();
for (int i = 0; i < string.length(); i++) {
set.add(string.charAt(i));
}
if(string.length() != set.size()){
return true;
}
return false;
}
/**
* This method takes in a string and checks for duplicates (including spaces) but
* is case sensitive (A & a = false)
* @param stringToCheck
* @return
*/
public static boolean hasDuplicateNoCase(String stringToCheck) {
Set<Character> set = new HashSet<>();
for (int i = 0; i < stringToCheck.length(); i++) {
set.add(stringToCheck.charAt(i));
}
if(stringToCheck.length() != set.size()){
return true;
}
return false;
}
//use regex with replaceAll method
/**
*
* @param stringToCheck
* @return true if no characters are duplicate even if there are spaces (case-sensitive)
*/
public static boolean hasDuplicateNotSpaces(String stringToCheck){
String noSpaces = stringToCheck.replaceAll("\\s", "");
Set<Character> set = new HashSet<>();
for (int i = 0; i < noSpaces.length(); i++) {
set.add(noSpaces.charAt(i));
}
if(noSpaces.length() != set.size()){
return true;
}
return false;
}
}//end of class