leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit b9e5695db448fcd322c68cc6b5ef624afb8c70c4
parent edb1abf1565578a911bf6edf96a37d0c6b3399a9
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Tue,  2 May 2023 18:53:43 -0500

Completed first unique charcter in string problem using dart

Diffstat:
Afirst-unique-character-in-a-string/first-unique-character.dart | 24++++++++++++++++++++++++
1 file changed, 24 insertions(+), 0 deletions(-)

diff --git a/first-unique-character-in-a-string/first-unique-character.dart b/first-unique-character-in-a-string/first-unique-character.dart @@ -0,0 +1,24 @@ +//Find the first character in a string that is not recurring then return the index. +//The time complexity of this code is O(n) where n is the number of charcters in the string. +//Time: 297ms Beats: 83.58% +//Memory: 144.1MB Beats: 41.79% + +class Solution { + int firstUniqChar(String s) { + Map<String, int> letters = {}; + for(int i = 0 ; i < s.length ; ++i){ + if(letters.containsKey(s[i])){ + letters[s[i]] = 2; + } + else{ + letters[s[i]] = 1; + } + } + for(int i = 0 ; i < s.length ; ++i){ + if(letters[s[i]] == 1){ + return i; + } + } + return -1; + } +}