Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest algo for searching set of characters in given string

Tags:

java

algorithm

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

like image 420
Taranfx Avatar asked Feb 01 '11 08:02

Taranfx


1 Answers

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.

like image 109
Oliver Charlesworth Avatar answered Sep 21 '22 23:09

Oliver Charlesworth