1

isalnum(char) is a method which tells us, whether a given character is alphanumeric or not. What is the time complexity of this small function?

I have written a small subroutine :

bool check(string s,int i)
     {
        if((s[i]>='a' && s[i]<='z') ||
           (s[i]>='0' && s[i]<='9') ||
          (s[i]>='A' && s[i]<='Z'))
        {
            return true;
        }     
        return false;
    }

Is the above subroutine works same as isalnum(char) method? Are there time complexities same?

Ankit
  • 13
  • 2
  • It is O(1) by itself: *there is no loop, explicit or implicit* — see https://en.wikipedia.org/wiki/Big_O_notation – user2864740 Jan 27 '22 at 06:06
  • The time complexity may be `O(1)` but that is not the most efficient way of implementing it. Thinking of swapping time for space (most machines have lots of space so if you want faster you can give some up, if your machine does not have lots of space this is fine). – Martin York Jan 27 '22 at 06:14
  • The complexity of `isalnum()` is unspecified by the C++ (and C) standards. Even with the required locale support, it would not be difficult to imagine it would involve a fixed number of steps, so be O(1) - meaning its (worst-case) run time would be proportional to the number of times it is called. It might be slower than your implementation, but your implementation does not include locale support - at most, all that you've affected is the proportional constant, but you've also lost localisation support. – Peter Jan 27 '22 at 07:58
  • What is the parameter of time complexity? You want to check 1 character, so not the length of an inpug string? – Sebastian Jan 27 '22 at 08:10
  • 2
    As an aside, this code assumes that `char` is encoded in ASCII. Not all computers do that. – BoP Jan 27 '22 at 10:12
  • The typical implementation of the character classification functions uses an array with bit flags for the various properties of the character at that position. So `isalpha(x)` is simply `return table[x] & ALPHA;`, `isdigit(x)` is `return table[x] & DIGIT;`, and `isalnum(x)` is `return table[x] & (ALPHA | DIGIT);`. Locale changes just change the value of `table` to point at a different array. So, time complexity is O(1), and you're not going to write something that's faster, even if it has the same time complexity (as yours does). – Pete Becker Jan 27 '22 at 14:08

1 Answers1

4

These functions are different, because isalnum takes int. If you ask about the actions they perform, they are also different.

isalnum('\xdf'), default C locale, returns false
isalnum('\xdf'), ISO-8859-1 locale, returns true
check("\xdf", 0) always returns false.

Time complexities in the both cases are similar, O(1). Your function may be faster, since it does not use locales.

273K
  • 29,503
  • 10
  • 41
  • 64
  • When I used my own function to check "whether given char is alphanumeric", the program gave TLE. But when I used the alphanum() method, it worked fine. If my program was supposed to be faster then why my program was getting into TLE. Also what is locale? where can I read about it more? – Ankit Jan 29 '22 at 11:13
  • @Ankit What is `alphanum`? The same as `isalnum` or as `check`? Is TLE timeout? Time complexity (as given by the Big-O notation) is not exactly the same as being faster. It is a limit for an input value (typically size) going to infinite. It is especially helpful for larger data. Locale: https://en.wikipedia.org/wiki/Locale_(computer_software) Perhaps the runtime depends on how you used the function, there could be an infinite loop. – Sebastian Jan 29 '22 at 11:19