Solve a few String questions

Valid Anagram

This one is a classic: `sorted(a) == sorted(b)`

Group Anagrams

Using product of prime numbers to create a key for anagrams is much faster than
sorting the characters in each word. It is also satisfyingly simple.

Encode and Decode Strings

My initial implementation was clumsy and prone to fail for edge-cases. A more
elegant solution is using something like:

```python
def encode(words):
  return "".join("{}:{}".format(len(x), x) for x in words)
```
This commit is contained in:
William Carroll 2020-12-25 03:52:54 +00:00
parent c34a3e2e97
commit 93d7b5d8ea

View file

@ -111,9 +111,9 @@
https://leetcode.com/problems/longest-repeating-character-replacement/
** TODO Minimum Window Substring
https://leetcode.com/problems/minimum-window-substring/
** TODO Valid Anagram
** DONE Valid Anagram
https://leetcode.com/problems/valid-anagram/
** TODO Group Anagrams
** DONE Group Anagrams
https://leetcode.com/problems/group-anagrams/
** DONE Valid Parentheses
https://leetcode.com/problems/valid-parentheses/
@ -123,7 +123,7 @@
https://leetcode.com/problems/longest-palindromic-substring/
** TODO Palindromic Substrings
https://leetcode.com/problems/palindromic-substrings/
** TODO Encode and Decode Strings (Leetcode Premium)
** DONE Encode and Decode Strings (Leetcode Premium)
https://leetcode.com/problems/encode-and-decode-strings/
* Tree
** DONE Maximum Depth of Binary Tree