Skip to content

Commit

Permalink
Added some code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed Jul 26, 2023
1 parent 878d801 commit 51d1061
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
24 changes: 12 additions & 12 deletions leetcode/problems/c/find-the-difference.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// by @codeAbinash
// Time Complexity : O(m + n), m = length of s, n = length of t
// Space Complexity : O(1)
// Time : O(n)
// Space : O(1)

char findTheDifference(char * s, char * t){
int i = 0, hash[26] = {0};
while(s[i]) hash[s[i++] - 'a']++;
i = 0;
while(t[i]) hash[t[i++] - 'a']--;

for(i = 0; i < 26; i++)
if(hash[i] != 0)
return i + 'a';
return '\0';
char findTheDifference(char* s, char* t) {
int xor = 0;
int index = 0;
while (s[index] && t[index]) {
xor ^= s[index];
xor ^= t[index];
index++;
}
xor ^= t[index];
return xor;
}
22 changes: 22 additions & 0 deletions leetcode/problems/cpp/find-the-difference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

#include "iostream"
#include "vector"
using namespace std;

class Solution {
public:
char findTheDifference(string s, string t) {
int len = s.length();
int x = 0, index;
for (index = 0; index < len; index++) {
x ^= s[index];
x ^= t[index];
}
x ^= t[index];

return x;
}
};

0 comments on commit 51d1061

Please sign in to comment.