Leetcode 389. Find the Difference [Java] | GOOGLE coding interview question

preview_player
Показать описание
Check out how to solve the leetcode 389 Find the Difference question in Java. This is one of GOOGLE's most commonly asked coding interview questions according to Leetcode.

Please write in the comments below which leetcode problem you want me to solve next.

And remember, a leetcode a day keeps unemployment away!

Thanks for watching!
#leetcode389
Рекомендации по теме
Комментарии
Автор

A problem with this solution is there could be a possible integer overflow (very large strings). To overcome this, you can add a character at index “i” in “t” and immediately subtract character at index “i” in “s”.

When loop ends, you will be left with one character in “t”. Add it to your result.

successinsoftwareengineeri
Автор

class Solution {
public char findTheDifference(String s, String t) {

int sC = 0;
int tC = 0;

for(int i=0; i<s.length() + 1; i++){
if(i < sC.length()) sC += (int) s.charAt(i);

tC += (int) t.charAt(i);

}

int res = tC - sC;

return (char) res;

}
}

bekanaveriani