commit 8ba924436554cbda833edd926627269b7d3b3fcc parent b9e5695db448fcd322c68cc6b5ef624afb8c70c4 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Tue, 2 May 2023 19:09:12 -0500 Completed find the difference problem using dart Diffstat:
| A | find-the-difference/find-the-difference.dart | | | 26 | ++++++++++++++++++++++++++ |
1 file changed, 26 insertions(+), 0 deletions(-)
diff --git a/find-the-difference/find-the-difference.dart b/find-the-difference/find-the-difference.dart @@ -0,0 +1,26 @@ +//Find the character that was added to t. +//The input is guaranteed to have one character extra in +//string t and that value needs to be returned. +//The time complexity of this code is O(n) where n is the length +//of String t. +//Runtime: 224ms Beats: 100% +//Memory: 142MB Beats: 47.62% + + +class Solution { + String findTheDifference(String s, String t) { + Map<String,int> characters = {}; + for(int i = 0 ; i < s.length ; ++i){ + characters[s[i]] = (characters[s[i]] ?? 0) + 1; + } + for(int i = 0 ; i < t.length ; ++i){ + if((characters[t[i]] ?? 0) == 0 ){ + return t[i]; + } + else{ + characters[t[i]] = (characters[t[i]] ?? 0)- 1; + } + } + return ""; + } +}