File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
contest/src/main/java/com/github/contest/hashTable Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -116,4 +116,35 @@ fun tupleSameProduct(nums: IntArray): Int {
116
116
if (count > 1 ) tuples + = count * (count - 1 ) * 4
117
117
}
118
118
return tuples
119
+ }
120
+
121
+ /* *
122
+ * 1796. Second Largest Digit in a String
123
+ */
124
+
125
+ fun secondHighest (s : String ): Int {
126
+ if (s.hasSingle()) return - 1
127
+ val map = mutableMapOf<Int , Int >()
128
+ for (char in s) {
129
+ if (isDigit(char)) {
130
+ val value = char.digitToInt()
131
+ map[value] = map.getOrDefault(value, 0 ) + 1
132
+ }
133
+ }
134
+ var keyMax = 0
135
+ for (key in map.keys) {
136
+ keyMax = maxOf(keyMax, key)
137
+ }
138
+ if (map.contains(keyMax)) map.remove(keyMax)
139
+
140
+ return map.keys.maxOrNull() ? : - 1
141
+ }
142
+
143
+ private fun String.hasSingle (): Boolean = when {
144
+ this .length == 1 -> true
145
+ else -> false
146
+ }
147
+
148
+ private fun isDigit (s : Char ): Boolean {
149
+ return s in " 0123456789"
119
150
}
You can’t perform that action at this time.
0 commit comments