This is a debate I was having with one of my friends: What would be the fastest way of making a valiation method that checks if the given string has one of the non-allowed characters
Method I: simple
char [] invalidChars = "!@#$%^...".toCharArray();
for (int i = 0; i < myString.length(); i++) {
char ch = myString.charAt(i);
for (int j = 0; j < invalidChars.length; j++) {
if (invalidChars[j] == ch) {
return false;
}
}
}
Method II: Exploiting Map's O(1)
Map <String,String> map = new HashMap<String, String>();
map.put("!", null);
map.put("@", null);
map.put("#", null);
map.put("$", null);
map.put("^", null);
...
for (int i = 0; i < labels.length(); i++) {
char ch = labels.charAt(i);
if (map.containsKey(ch)) {
return false;
}
return true;
}
The method I is actually N2 but as good as N when invalidChars are less in number. What should be preferred when Case I: There are lots of invalid chars, Case II: only few invalid chars?
Note: I'm not looking for any inbuilt java solutions but, just the algorithm to filter few (not all) non-text characters
If you're only interested in validating ASCII characters, then a length-128 boolean lookup-table may be faster than either of the above methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With