Problem description: You are given a number. You can swap two adjacent digits, if they are both odd or even. For e.g. given (3,8), you can’t swap them. But you can swap (5,9) since they are both odd. They have the same parity.
A similar problem is explained here https://www.youtube.com/watch?v=qEIGhVtZ-sg
Function description:
swapDigitsAndGetLargestNumber(number: String)
Returns:
Largest number formed from the string.
Example 1
Input: 117596801
Output: 975118601
Example 2
Input: 68019
Output: 86091
Keep in mind, you can only swap if they have the same parity.
Pseudo code
- Convert the string into a array of numbers
- Start with the left most digit.
- Get the current plus one digit.
- Check if the digit is odd or even.
- If both digits are odd or even, and current digit is greater than the next digit, we need to swap these two digits.
- Let’s swap these digits.
- Next call the iterative method again recursively.
- Once all the digits are swapped, we can return the highest number.
func swapDigitsAndGetLargestNumber(num: String) -> String {
var intArray:[Int] = [Int]()
for char in num {
intArray.append(char.wholeNumberValue ?? 0)
}
return keepIterating(num: num.compactMap{$0.wholeNumberValue}, string: num)
}
func keepIterating(num: [Int], string: String) -> String {
var currentIndexValue = 0
var nextIndexValue = 0
var nextIndex = 0
for idx in 0 ..< num.count {
currentIndexValue = num[idx]
nextIndex = idx + 1
if nextIndex < num.count {
nextIndexValue = num[nextIndex]
}
if currentIndexValue % 2 == 0 && nextIndexValue % 2 == 0 {
// this is even
// can be tested
if currentIndexValue < nextIndexValue {
//print("even \(currentIndexValue) \(nextIndexValue) needs to shifted")
let (swappedInArray, swappedString) = getSwappedArray(string: string, idx: idx, nextIndex: nextIndex)
return keepIterating(num: swappedInArray, string: swappedString)
}
} else if currentIndexValue % 2 != 0 && nextIndexValue % 2 != 0 {
// this is odd
if currentIndexValue < nextIndexValue {
//print("odd \(currentIndexValue) \(nextIndexValue) needs to shifted")
let (swappedInArray, swappedString) = getSwappedArray(string: string, idx: idx, nextIndex: nextIndex)
return keepIterating(num: swappedInArray, string: swappedString)
}
}
}
return string
}
func getSwappedArray(string: String, idx: Int, nextIndex: Int) -> ([Int], String) {
let swappedString = swapCharacters(string: string, currentIndex: idx, nextIndex: nextIndex)
//print("swapped String \(swappedString)")
let swappedArray = Array(swappedString)
var swappedInArray:[Int] = [Int]()
for char in swappedArray {
swappedInArray.append(char.wholeNumberValue!)
}
return (swappedInArray, swappedString)
}
func swapCharacters(string: String, currentIndex: Int, nextIndex: Int) -> String {
var characters = Array(string)
characters.swapAt(currentIndex, nextIndex)
return String(characters)
}
func getLargestNumber(num: String) -> String {
var intArray:[Int] = [Int]()
for char in num {
intArray.append(char.wholeNumberValue ?? 0)
}
return keepIterating(num: num.compactMap{$0.wholeNumberValue}, string: num)
}
Leave a Reply