leetcode

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

commit 42e51f71841bcbeeec9e562a769b414948f63391
parent bbb1b06f9a1f3cd45aac5f615ae19a78c1148366
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Tue, 18 Apr 2023 01:08:05 -0500

Non repeating characters in dart

Diffstat:
Afirst-non-repeating-character/first-non-repeating.dart | 20++++++++++++++++++++
1 file changed, 20 insertions(+), 0 deletions(-)

diff --git a/first-non-repeating-character/first-non-repeating.dart b/first-non-repeating-character/first-non-repeating.dart @@ -0,0 +1,20 @@ +import 'dart:io'; +//This is a leetcode style question, but not from leetcode. +//The idea is to find the first character of the input string that +//does not repeat in the string. This is the optimized way using maps +//which is linear time, but the simple solution would be brute force checking +//every single charcter which would be O(n^2); +void main(){ + stdout.write("Enter text to find the first non-repeating character: "); + String s = stdin.readLineSync() ?? "N/A"; + Map<String,int> vals = {}; + for(int i = 0 ; i < s.length; ++i){ + vals[s[i]] = (vals[s[i]] ?? 0) + 1; + } + for(int i = 0 ; i < s.length ; ++i){ + if(vals[s[i]] == 1){ + print(s[i] + " is the first character that does not repeat in the string."); + break; + } + } + }