leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

find-the-difference.dart (705B)


      1 //Find the character that was added to t.
      2 //The input is guaranteed to have one character extra in
      3 //string t and that value needs to be returned.
      4 //The time complexity of this code is O(n) where n is the length
      5 //of String t.
      6 //Runtime: 224ms Beats: 100%
      7 //Memory: 142MB Beats: 47.62%
      8 
      9 
     10 class Solution {
     11   String findTheDifference(String s, String t) {
     12     Map<String,int> characters = {};
     13     for(int i = 0 ; i < s.length ; ++i){
     14       characters[s[i]] = (characters[s[i]] ?? 0) + 1;
     15     }
     16     for(int i = 0 ; i < t.length ; ++i){
     17       if((characters[t[i]] ?? 0) == 0 ){
     18         return t[i];
     19       }
     20       else{
     21         characters[t[i]] = (characters[t[i]] ?? 0)- 1;
     22       }
     23     }
     24     return "";
     25   }
     26 }