ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Check Palindrome

Objective-C

Goal -- WPM

Ready
Exercise Algorithm Area
1#import <Foundation/Foundation.h>
2
3// Helper to check if a character is alphanumeric
4BOOL isAlphanumeric(unichar c) {
5return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
6}
7
8// Helper to convert character to lowercase
9unichar toLower(unichar c) {
10if (c >= 'A' && c <= 'Z') {
11return c - ('A' - 'a');
12}
13return c;
14}
15
16// Function to check if a string is a palindrome
17BOOL isPalindrome(NSString *s) {
18if (s == nil || s.length == 0) {
19return YES; // An empty string is considered a palindrome
20}
21
22NSInteger left = 0;
23NSInteger right = s.length - 1;
24
25while (left < right) {
26unichar leftChar = [s characterAtIndex:left];
27unichar rightChar = [s characterAtIndex:right];
28
29// Skip non-alphanumeric characters from the left
30if (!isAlphanumeric(leftChar)) {
31left++;
32continue;
33}
34
35// Skip non-alphanumeric characters from the right
36if (!isAlphanumeric(rightChar)) {
37right--;
38continue;
39}
40
41// Compare lowercase versions of the characters
42if (toLower(leftChar) != toLower(rightChar)) {
43return NO; // Mismatch found, not a palindrome
44}
45
46// Move pointers inward if characters match
47left++;
48right--;
49}
50
51return YES; // All comparable characters matched
52}
53
54int main(int argc, const char * argv[]) {
55@autoreleasepool {
56NSString *testStr1 = @"A man, a plan, a canal: Panama";
57NSLog(@"String: %@, Is Palindrome: %s", testStr1, isPalindrome(testStr1) ? "YES" : "NO"); // Expected: YES
58
59NSString *testStr2 = @"race a car";
60NSLog(@"String: %@, Is Palindrome: %s", testStr2, isPalindrome(testStr2) ? "YES" : "NO"); // Expected: NO
61
62NSString *testStr3 = @"";
63NSLog(@"String: %@, Is Palindrome: %s", testStr3, isPalindrome(testStr3) ? "YES" : "NO"); // Expected: YES
64
65NSString *testStr4 = @"a.";
66NSLog(@"String: %@, Is Palindrome: %s", testStr4, isPalindrome(testStr4) ? "YES" : "NO"); // Expected: YES
67
68NSString *testStr5 = @"Madam";
69NSLog(@"String: %@, Is Palindrome: %s", testStr5, isPalindrome(testStr5) ? "YES" : "NO"); // Expected: YES
70}
71return 0;
72}
Algorithm description viewbox

Check Palindrome

Algorithm description:

This Objective-C code checks if a given string is a palindrome, ignoring case and non-alphanumeric characters. It uses a two-pointer approach, starting from both ends of the string and moving inwards. The pointers skip over any characters that are not letters or numbers. If the alphanumeric characters match when converted to lowercase, the string is considered a palindrome. This algorithm is widely used in text processing, data validation, and competitive programming.

Algorithm explanation:

The `isPalindrome` function determines if a string reads the same forwards and backward, considering only alphanumeric characters and ignoring case. It employs a two-pointer technique. `left` starts at the beginning of the string, and `right` starts at the end. The `while` loop continues as long as `left` is less than `right`. Inside the loop, helper functions `isAlphanumeric` and `toLower` are used. The `left` pointer advances past any non-alphanumeric characters, and similarly, the `right` pointer retreats past non-alphanumeric characters. Once both pointers are on alphanumeric characters, they are converted to lowercase and compared. If they do not match, the function immediately returns `NO`. If they match, both pointers move one step closer to the center. If the loop completes without finding any mismatches, it means the string is a palindrome, and the function returns `YES`. Edge cases like empty strings or strings with only non-alphanumeric characters are handled correctly, returning `YES`. The time complexity is O(n), where n is the length of the string, because in the worst case, each character is examined once. The space complexity is O(1) as it uses a constant amount of extra space for the pointers and temporary variables.

Pseudocode:

function isPalindrome(string s):
  if s is empty or nil:
    return true

  left = 0
  right = length of s - 1

  while left < right:
    while left < right and s[left] is not alphanumeric:
      increment left
    while left < right and s[right] is not alphanumeric:
      decrement right

    if toLower(s[left]) is not equal to toLower(s[right]):
      return false

    increment left
    decrement right

  return true