Skip to content

Commit 8adb8c0

Browse files
add 242
1 parent f5d3e1e commit 8adb8c0

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt

+20-1
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,30 @@ fun minimumOperations(nums: IntArray): Int {
305305
}
306306
}
307307

308-
return if (map.size > 0) operations + 1 else operations
308+
return if (map.isNotEmpty()) operations + 1 else operations
309309

310310
}
311311

312312
private fun MutableMap<Int, Int>.reduceOrRemove(key: Int) {
313313
this[key] = this.getOrDefault(key, 0) - 1
314314
if (this.getOrDefault(key, 0) == 0) this.remove(key)
315+
}
316+
317+
/**
318+
* 242. Valid Anagram
319+
*/
320+
321+
fun isAnagram(s: String, t: String): Boolean {
322+
if (s.length != t.length) return false
323+
val first = IntArray(26)
324+
val second = IntArray(26)
325+
326+
for (char in s) first[char - 'a']++
327+
for (char in t) second[char - 'a']++
328+
329+
for (char in t) {
330+
if (first[char - 'a'] == 0 || first[char - 'a'] != second[char - 'a']) return false
331+
}
332+
333+
return true
315334
}

0 commit comments

Comments
 (0)