aboutsummaryrefslogtreecommitdiff
path: root/find_difference.c
blob: 948bbd595883cc98a2b17373305ef573ca420368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 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
}