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 |
Sherlock and Array | O(N) | O(N) | Easy | Search | 40.0 | 40.0 | |
Ice Cream Parlor | O(N) | O(N) | Easy | Search | 30.0 | 30.0 | |
Maximise Sum | O(N) | O(NlgN) | Moderate | Search | 20.31 | 65.0 | |
Missing Number | O(N) | O(N) | Moderate | Search | Do you believe 0.59s overtime? | 25.99 | 45 |
Data Structures in HackerRankHelper
- FIFO Queue with Two Arraies
- FIFO Queue with Listnode
- ListNode
- AVLNode
- AVLTree
- BinaryIndexedTree
- Heap
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()
If you want to use this module to play with local test files, you might need to import this module. Swift Package Manager is a good tool.
Firstly, add following Package.swift
to your root directory.
import PackageDescription
let package = Package(
name:"YourModuleName",
dependencies: [
.Package(url:"https://github.com/jindulys/HackerRankSolutions.git", majorVersion:1),
]
)
Secondly, when you use this module in your source file, you should:
import HRSwift
Then run swift build
, and .build/debug/YourModuleName
, Alright, Run!!
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, which is used for Hackerrank submission. This module could be imported by other packages, so make your class and those usable methods public. 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 new 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