Main concept is to revert given list and then compare reverted and original.
If palindrome, reverted == original
Problem is quite simple, no description required
Main issue was memory management: each component of tree has multiple references on it,
so each TreeNode should be a Rc<RefCell<T>>
instance.
Rc
is container for Reference Counted pointer, RefCell
for dynamic-checked borrowing
Inorder (or infix order) is when firstly left subtree is taken then root and lasting with right subtree
Rc
RefCell
Task
Solution
Palindrome number is when iterating a number from end and from start we have the same digits Main difficulty is to iterate number in two directions without string or vector usage. To iterate from bigger to lower digits,
- take digit from current position:
digit = number / 10 ** (digit_count - 1)
- go to next iteration
number -= digit * 10 ** digit_count
To iterate from lower to upper digits,
- take digit:
digit = number % 10
- go to next iteration
number /= 10
Quite easy task, no explanations required
This task bases on hashtable usage. As soon as we have to find a common
prefix we can easily take random string from string pool and split it into hashtable
I prefer take the first string from pool.
Example: string test
Hashtable will be:
key | value |
---|---|
t | 1 |
te | 1 |
tes | 1 |
test | 1 |
We place 1 instead of 0 because at least in one string this prefix is presented (in the first in my example or in random) Then we have to iterate other the pool matching whether prefix is the beginning of each string If some string in the pool matches we increase in hashtable corresponding entry's value
Example: if pool is [test, tears, technical]
then hashtable after all iterations will be
key | value |
---|---|
t | 3 |
te | 3 |
tes | 1 |
test | 1 |
And the last step is to find in hashtable an entry which has as large value as is size
of the pool and then take entry with longest key.
This longest key will be longest common prefix
The idea is quite simple: use Vec<char>
as a stack where we store opening parentheses.
Iterating through the string we match whether symbol is opening or closing parenthesis
If it is opening parenthesis add it to stack If it is closing parenthesis:
- pop from stack last opening parenthesis
- if stack is empty
return false
- find a closing match for this opening parenthesis (like
'('
goes to')'
, etc) - if closing parenthesis for popped open one isn't the same as current character
return false
When all string is done, return the fact whether stack is empty (obviously it should be empty if string is valid: every opening parenthesis was matched with closing)
Task requires simple recursion and a lot of is-else branches
Task is solved with recursion and is completely like previous task
Task requires two hashmap-like arrays (arrays used for memory optimisation)
We use index of 26-sized array as letter identifier
First array is used to storage occurrences count
Second is used to storage last occurrence index
Thus last occurrence index for unique element is the desired one we have to take this index
Note as soon as we'll iterate through characters, your solution should give the lowest index but not the most first letter's in alphabet index
If unique letter c
will have index 4
, unique letter l
will have 0, we should output index for l
, not the c
, thus they are both unique
Task
Solution
Task is quite simple no description needed
As a hashmap we manage criteria characters.
For each character we store count of occurrences in criteria.
When iterating through words, each letter of each word can be ether
- be in criteria hashmap and there's one more occurrence and this letter "take" this occurrence (decrement in hashmap value)
- be in criteria hashmap but all occurrence are taken by previous these letters. Mark this word as bad one
- not be in criteria. All this word will be marked as bad
Once iteration through a single word is done mark it as a good one
Use a hashmap to store dictionary and then simple iterate with lexicographical order
Build a pascal's triangle of given row size Task Solution
Use set to define whether there's cycle so number is not happy Task Solution
Simple intuitive solution Task Solution
Implement simple uniqueness-checked container Source