We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 64c8d4e commit 08c72fdCopy full SHA for 08c72fd
scripts/algorithms/S/Second Largest Digit in a String/Second Largest Digit in a String.py
@@ -1,11 +1,11 @@
1
+# Runtime: 80 ms (Top 5.3%) | Memory: 16.30 MB (Top 72.1%)
2
+
3
class Solution:
4
def secondHighest(self, s: str) -> int:
- s=set(s)
- a=[]
5
- for i in s:
6
- if i.isnumeric() :
7
- a.append(int(i))
8
- a.sort()
9
- if len(a)<2:
10
- return -1
11
- return a[len(a)-2]
+ nums = []
+ for char in s:
+ if char.isdigit():
+ nums.append(int(char))
+ nums = [num for num in nums if num != max(nums)]
+ if len(nums) >= 1: return max(nums)
+ else: return -1
0 commit comments