389. Find the Difference
use HashMap
T: O(n)
S: O(26)
class Solution {
public char findTheDifference(String s, String t) {
if (s.length() == 0) {
return t.charAt(0);
}
char res = '\0';
char[] map = new char[26];
for (char c : t.toCharArray()) {
map[c - 'a']++;
}
for (char c : s.toCharArray()) {
map[c - 'a']--;
}
for (int i = 0; i < 26; i++) {
if (map[i] != 0) {
res = (char)(i + 'a');
}
}
return res;
}
}use bit

T: O(n)
S: O(1)
Previous1284. Minimum Number of Flips to Convert Binary Matrix to Zero MatrixNext3. Longest Substring Without Repeating Characters
Last updated
Was this helpful?