tomkowz
BAN USER
extension String {
func permutations() -> Int {
let len = self.characters.count
guard len > 1 else { return len }
var count = 0
permutations(string: self, pos: 0, len: len, count: &count)
return count
}
private func isValid(string: String, start: Int, end: Int) -> Bool {
let startIdx = string.index(string.startIndex, offsetBy: start)
let endIdx = string.index(string.startIndex, offsetBy: end)
let candidate = string[startIdx..<endIdx]
let num = Int(candidate)!
return num > 0 && num <= 26
}
private func permutations(string: String, pos: Int, len: Int, count: inout Int) {
if pos + 1 == len {
count = count + 1
return
} else if isValid(string: string, start: pos, end: pos + 1) {
permutations(string: string, pos: pos + 1, len: len, count: &count)
}
if pos + 2 == len && isValid(string: string, start: pos, end: pos + 2) {
count = count + 1
} else if isValid(string: string, start: pos, end: pos + 2) {
permutations(string: string, pos: pos + 2, len: len, count: &count)
}
}
}
"1234".permutations() // 3
Swift:
func existsContiguousSubArray(array: [Int], sum: Int) -> Bool {
guard array.count > 0 else { return false }
var start = 0
var end = 0
var result = 0
while start <= end && end < array.count {
result = result + array[end]
if result == sum {
return true
} else if result > sum {
result -= array[start]
start = start + 1
}
end = end + 1
}
return false
}
Swift:
func moveToFront(idx: Int, array: inout [Int]) {
let num = array[idx]
array.remove(at: idx)
array.insert(num, at: 0)
}
func moveToFrontSort(array: inout [Int]) {
guard array.count > 1 else { return }
for sortCursor in 0..<array.count {
var maxIdx = sortCursor
for idx in maxIdx..<array.count {
if array[idx] > array[maxIdx] {
maxIdx = idx
}
}
moveToFront(idx: maxIdx, array: &array)
}
}
var array = [12, 3, 4, 24, 22, 4, 224, 453, 232]
moveToFrontSort(array: &array)
Here is a solution using DFS and then comparing two arrays of leaves. Written in Swift.
Also another solution could be:
Create a generator of leaves based on a tree and ask for leaves one by one, and instead of creating array of leaves you could stop after getting two different leaves. This way you could avoid traversing through entire tree and stop right after detecting a difference.
import Foundation
class Tree {
typealias LeavesPair = (leaf1: String?, leaf2: String?)
private class Node {
let val: String
var left: Node?
var right: Node?
init(val: String) {
self.val = val
}
}
private var root: Node?
static func findDifferenceInLeaves(tree1: Tree, tree2: Tree) -> LeavesPair {
var leaves1 = [Node]()
var leaves2 = [Node]()
collectLeaves(root: tree1.root, leaves: &leaves1)
collectLeaves(root: tree2.root, leaves: &leaves2)
let minCount = min(leaves1.count, leaves2.count)
for idx in 0..<minCount {
if leaves1[idx].val != leaves2[idx].val {
return LeavesPair(leaves1[idx].val, leaves2[idx].val)
}
}
return LeavesPair(nil, nil)
}
init(array: [String?]) {
array.flatMap({ $0 }).forEach { insert(root: &root, val: $0) }
}
func insert(val: String) {
insert(root: &root, val: val)
}
private func insert(root: inout Node?, val: String) {
if let root = root {
if val < root.val {
if root.left != nil {
insert(root: &root.left, val: val)
} else {
root.left = Node(val: val)
}
} else {
if root.right != nil {
insert(root: &root.right, val: val)
} else {
root.right = Node(val: val)
}
}
} else {
root = Node(val: val)
}
}
private static func collectLeaves(root: Node?, leaves: inout [Node]) {
guard let root = root else { return }
if root.left == nil && root.right == nil {
leaves.append(root)
}
collectLeaves(root: root.left, leaves: &leaves)
collectLeaves(root: root.right, leaves: &leaves)
}
}
// Test
let arr1: [String?] = ["A", "B", "C", "D", "E", nil, nil]
let tree1 = Tree(array: arr1)
let arr2: [String?] = ["A", "D", "B"]
let tree2 = Tree(array: arr2)
let diff = Tree.findDifferenceInLeaves(tree1: tree1, tree2: tree2)
RepKeithyMayes, Area Sales Manager at ASAPInfosystemsPvtLtd
Keithy , an administrative manager , Designs and access to technology to facilitate new infrastructure for students. and I have a hobby ...
RepAayshaRosie, HTML Freshers at Abs india pvt. ltd.
Aaysha , a book editor with 4 years of experience with increasing readership and developing skilled writers. At the Daily Record ...
Repirenereed98, Discount Furniture Showcase at Physical therapist aid
I'm a Physical advisor helping working at Discount Furniture Showcase . I'm likewise a film buff who partakes in ...
RepNaomiAdams, abc at 8x8
Passionate educator for nearly 5 years with a strong desire to help students recognize the connection between learning and experience ...
RepLiamLee, abc at ADP
Proven leadership skills that have helped projects get completed within the time and budget constraints, Dedication to following appropriate safety ...
Repzk6354367, Financial Software Developer at Agilent Technologies
Hi , I am Zessie from the USA , working as Dien for the last three years. Previously I have served as ...
Repreiviasmith, Malwarebytes customer service at ABC TECH SUPPORT
I am Reivia an outgoing and motivated Travel Consultant with over 5 years of experience in delivering professional travel related ...
RepSamiyahKate, Java Experienced at Agilent Technologies
I am Samiyah, having 3 years of experience in assessing, diagnosing, screening, and preventing language, speech, and swallowing disorders. I ...
RepSharonSwann, AT&T Customer service email at A9
Bilingual, self-motivated Air Hostess with a proven record of providing excellent customer service and exceeding all corporate and personal expectations ...
RepGizzyKale, Graphics Programmer at Arista Networks
Je suis Gizzy, un commis aux archives avec d'excellentes compétences interpersonnelles et de communication. Expert en nourriture , boisson et ...
RepMoniKim, Animator at Clean Power Research
I am Moni , a reliable Special Projects/Order Filler people oriented personality with great organizational and interpersonal communication skills. I ...
Repharveyoberion, Analyst at AMD
Hi, I am Harvey, from the USA. I am working as a soil scientist. I study soil as a natural ...
Repmarilyannrandall, Backend Developer at ABC TECH SUPPORT
As a Data input clerk, I deal with customers on a daily basis. Entering customer and account data from source ...
Repkany7635, optician at AMMB
I am a detail oriented and knowledgeable optical assistant with 2 years of experience in an optometry office setting. Collect ...
Repjoeevansjoe6, Repairer at Monlinks
I have been functioning as a repairman at monlinks organization for a long time . Here I learn numerous things . My ...
RepRebecaGomes, Amazon Customer Service Number +1-808.800.0449 at ABC TECH SUPPORT
A printmaker designs and creates prints using techniques such as woodcuts or silkscreen, which are usually transferred to surfaces using ...
Repearleneefranks, Accountant at 247quickbookshelp
I am working as an Information clerk and I love my job. I also love to read new Articles related ...
RepZoeyUhl, abc at 247quickbookshelp
With a highly experienced law clerk, well respected and writing proficient, detail oriented and proficient in preparing legal memos of ...
RepTerryMorales, Accountant at ADP
I assist other social and human service providers in providing client services in a wide variety of fields, such as ...
Repjudithasmith175, Accountant at Akamai
Hey, my name is Terry and I completed all my study from California. And Nowadays I am working as a ...
- tomkowz October 05, 2016