leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

find_difference.c (602B)


// 389. Find the Difference
#include <stdio.h>

/*
 * given two strings 's' and 't'. string 't' is generated by random shuffling
 * string 's' and then add one more letter at a random position. return the
 * letter that was added to 't'.
 */

char findTheDifference(char *s, char *t) {
  char ans = 0;
  while (*s != '\0') {
    ans ^= (*s) ^ (*t);
    ++s;
    ++t;
  }
  return ans ^ (*t);
}

int main() {
  char s1[] = {"abcd"}, t1[] = {"abcde"};
  char s2[] = {""}, t2[] = {"y"};
  printf("%c\n", findTheDifference(s1, t1)); // expect: e
  printf("%c\n", findTheDifference(s2, t2)); // expect: y
}