Try to solve problems in JavaScript, Python, and Java.
This is TIL(memo of key learnings) for solving problems in these languages,
but mostly python.
241217
an anagram is a word or phrase formed by rearranging the characters of another word or phrase.
"listen" is an anagram of "silent."
with Counter in python: https://docs.python.org/3/library/collections.html#collections.Counter
Approach A. Check Character Frequencies
Approach B. Compare Frequency Counters

Strings are iterable in Python because they are sequences of characters.
any object is considered iterable if it:
__iter__ method__getitem__ method for accessing elements by index.use Counter to count the character frequencies for both the original and target strings.
The == operator checks for equality of content, not memory location.
Two Counter objects are considered equal if they contain the exact same counts for all elements, regardless of order.
__iter__ or __getitem__ method.unlike python & js, java does not support indexing with square brackets [index] for strings.

π java doesnβt allow operator overloading. instead, use the charAt() method
String example = "listen";
char firstChar = example.charAt(0); // l
HashMap vs js's Map| Functionality | Java HashMap | JavaScript Map |
|---|---|---|
| Get | .get(key) .getOrDefault(key, defaultValue) | .get(key) |
| Insert/Update | .put(key, value) | .set(key, value) |
| Check for Keys | .containsKey(key) | .has(key) |
| Clear | .clear() | .clear() |
| Size | .size() (method) | .size (access property) |
| Iterate | for (Map.Entry<Key, Value> entry : map.entrySet()) | for (let [key, value] of map) |
javaScript Map allows method chaining, simplifies multiple operations:
map.set('key1', 'value1').set('key2', 'value2');
javaScript Map can be initialized directly during declaration. but java HashMap requires using .put() after object creation.
// js
const map = new Map([['key1', 'value1'], ['key2', 'value2']]);
// java
HashMap<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
javaScript Map preserves the insertion order of keys, whereas Java HashMap does not. (use LinkedHashMap in java for ordered HashMap)
specify the key and value types when declaring a HashMap to avoid type errors:
HashMap<Character, Integer> frequency = new HashMap<>();
