This is a repo for HackerRank Solutions in Swift
Different folders are named after corresponding domain names or contest names.
Problem | Time | Space | Difficulty | Tag | Note | Score | Max Score |
---|---|---|---|---|---|---|---|
Angry Professor | O(n) | Easy | Implementation | 20.0 | 20.0 | ||
SherlockAndTheBeast | O(n) | Easy | Implementation | 30.0 | 30.0 | ||
Utopian Tree | O(n) | Easy | Implementation | 20.0 | 20.0 | ||
Find Digits | O(n) | Easy | Implementation | 25.0 | 25.0 | ||
Funny String | O(n) | Easy | String | 25.0 | 25.0 | ||
Pangrams | O(n) | O(1) | Easy | String | 20.0 | 20.0 | |
Alternating Characters | O(n) | O(1) | Easy | String | 20.0 | 20.0 | |
Game of Thrones | O(n) | O(1) | Easy | String | 30.0 | 30.0 | |
Make it anagram | O(n) | O(1) | Easy | String | 30.0 | 30.0 | |
Anagram | O(n) | O(1) | Easy | String | 14.29 | 25 | |
Two Strings | O(n) | O(1) | Moderate | String | Set | 20.0 | 25.0 |
SherlockAndAnagrams | O(n^3lgn) | O(n^2) | Moderate | String | Sort, Dictionary, Permutation | 4.55 | 50.0 |
Palindrome Index | O(n) | O(1) | Easy | String | 25.0 | 25.0 | |
Sherlock And Valid String | O(n) | O(1) | Difficult | String | Dictionary | 100.0 | 100.0 |
Common Child | O(n^2) | O(n^2) | Difficult | String | Dynamic Programming | 50.0 | 60.0 |
The Maximum Subarray | O(n) | O(n) | Easy | Dynamic Programming | 16.67 | 50.0 | |
The Coin Change Problem | O(MN) | O(MN) | Medium | Dynamic Programming | bottom-up table construction | 60.0 | 60.0 |
Red John is Back | O(N) + O(nlgn) | O(N) | Moderate | Dynamic Programming | memoization | 54.17 | 65 |
Knapsack | O(MN) | O(MN) | Moderate | Dynamic Programming | memoization in swift | 54.06 | 60.0 |
Sam and SubStrings | O(N) | O(N) | Moderate | Dynamic Programming | 40.0 | 40.0 | |
The Longest Increasing Subsequence | O(N^2) | O(N) | Advanced | Dynamic Programming | time out | 26.67 | 60.0 |
Hexagonal Gird | O(N^2) | O(N) | Moderate | Dynamic Programming | recursive solution | 70.0 | 70.0 |
Sherlock and Cost | O(N) | O(N) | Moderate | Dynamic Programming | 28.13 | 50.0 | |
Dorsey Thief | O(NM) | O(NM) | Advanced | Dynamic Programming | 52.76 | 85.0 | |
Travel Around the World | O(N) | O(N) | Moderate | Dynamic Programming | validation pass and candidates finding pass | 120.0 | 120.0 |
Data Structures in HackerRankHelper
- FIFO Queue with Two Arraies
- FIFO Queue with Listnode
- ListNode
- AVLNode
- AVLTree
- BinaryIndexedTree
I created class for each problem. If you want to test those solution on HackerRank, please follow this guideline.
-
create a constant with class name.
let mySolution = MyClass()
-
call solution method on that constant
mySolution.solution()
-
copy and paste related helper methods from HackerRankHelper folder, eg. getLine(), getInt()
HackerRank lover, swift lover do not hesitate. There are tons of unsolved questions. If you want to contribute a little bit, please write your own swift solutions then make a pull request.
For each question, you should write your own Class in corresponding file, inside that Class define a solution method. For example, if you want to submit solution of Algorithms --> Strings --> Gemstones, you should write your solution inside String.swift. If there is no related file for a subdomain, please create a file.
Write a test case in HRSwiftTests.swift. You could use sample input and sample output from that question as a test.
func testAnagram() {
let test = Anagram()
XCTAssert(test.solve("aaabbb") == 3, "Pass")
XCTAssert(test.solve("ab") == 1, "Pass")
XCTAssert(test.solve("abc") == -1, "Pass")
}
If you write some general code that could be used afterwards, please write them in HackerRankHelper
Make sure your solution get a reasonable score on HackerRank then pull request.
You should obey Raywenderlich Swift Style Guide
All those solution I wrote used Swift1.2, since HackerRank only supports Swift1.* for now. But it is not a big deal since those solutions usually don't need too much new language features.