Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
leetcode
Manage
Activity
Members
Labels
Plan
Issues
29
Issue boards
Milestones
Code
Merge requests
29
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
John bedingfield
leetcode
Commits
2550a183
Commit
2550a183
authored
2 years ago
by
John bedingfield
Browse files
Options
Downloads
Patches
Plain Diff
387 solved using dictionary
parent
183dbd0e
No related branches found
Branches containing commit
Tags
v1.1.0
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
387_first_unique_char.py
+46
-0
46 additions, 0 deletions
387_first_unique_char.py
with
46 additions
and
0 deletions
387_first_unique_char.py
0 → 100644
+
46
−
0
View file @
2550a183
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 17 12:08:03 2022
@author: johnb aka John Bedingfield
"""
# this is for LeetCode #387, FIrst Unique Character in a String
# this was accepted
# runtime: 129 ms, faster than 68.27% of python3 submissions
# memory: 14.2MB, less than 20.47% of python3 submissions
class
Solution
:
def
firstUniqChar
(
self
,
s
:
str
)
->
int
:
counts
=
{}
# dictionary to hold a count of each letter
# loop throught the string and get a count of each letter
for
c
in
s
:
if
c
not
in
counts
:
counts
[
c
]
=
1
else
:
counts
[
c
]
=
counts
[
c
]
+
1
#print(counts)
# loop through the string and determine which char only has a count of 1
index
=
0
for
c
in
s
:
if
counts
[
c
]
==
1
:
return
index
else
:
index
+=
1
# if we made it through with no answer,
return
-
1
if
__name__
==
"
__main__
"
:
s
=
"
leetcode
"
s2
=
"
loveleetcode
"
s3
=
"
aabb
"
myS
=
Solution
()
starttime
=
int
(
time
())
print
(
myS
.
firstUniqChar
(
s3
))
endtime
=
int
(
time
())
totalTime
=
endtime
-
starttime
print
(
f
"
Total time was
{
totalTime
}
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment