-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4c8333
commit 90bf1a9
Showing
9 changed files
with
132 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
ktstrings/src/main/kotlin/com/kotlinground/strings/permutations/checkPermutations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.kotlinground.strings.permutations | ||
|
||
import kotlin.collections.set | ||
|
||
/** | ||
* Check if two strings are permutations of each other. Returns true if thy are false otherwise | ||
* | ||
* Complexity: | ||
* Time: O(nlog(n)) as sorting is used and sorting here is an O(nlog(n)) time complexity | ||
* Space: O(1) as no extra space is used to achieve checking for permutations | ||
*/ | ||
fun checkPermutationWithSorting(inputStr1: String, inputStr2: String): Boolean { | ||
// strings of different lengths cannot be permutations of each other | ||
if (inputStr1.length != inputStr2.length) { | ||
return false | ||
} | ||
|
||
val inputStr1Sorted = inputStr1.lowercase().split("").sorted() | ||
val inputStr2Sorted = inputStr2.lowercase().split("").sorted() | ||
|
||
for ((idx, _) in inputStr1Sorted.withIndex()) { | ||
if (inputStr1Sorted[idx] != inputStr2Sorted[idx]) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
/** | ||
* checks that two strings are permutations of each other and returns true if they are or false | ||
* otherwise. | ||
* Complexity: | ||
* Time: O(n) as it iterates through the strings once | ||
* Space: O(n) as it uses a map to store the frequency of each character in the strings | ||
* | ||
* */ | ||
fun checkPermutationWithMap(inputStr1: String, inputStr2: String): Boolean { | ||
// strings of different lengths cannot be permutations of each other | ||
if (inputStr1.length != inputStr2.length) { | ||
return false | ||
} | ||
|
||
val inputStr1Lower = inputStr1.lowercase() | ||
val inputStr2Lower = inputStr2.lowercase() | ||
|
||
val charCountMap = mutableMapOf<Char, Int>() | ||
|
||
for (char in inputStr1Lower) { | ||
if (charCountMap.containsKey(char)) { | ||
charCountMap[char] = charCountMap[char]!! + 1 | ||
} else { | ||
charCountMap[char] = 1 | ||
} | ||
} | ||
|
||
for (char in inputStr2Lower) { | ||
if (charCountMap.containsKey(char)) { | ||
charCountMap[char] = charCountMap[char]!! - 1 | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
for (count in charCountMap.values) { | ||
if (count != 0) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
2 changes: 1 addition & 1 deletion
2
...king/permutations/generatePermutations.kt → ...ings/permutations/generatePermutations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
ktstrings/src/test/kotlin/com/kotlinground/strings/permutations/CheckPermutationsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.kotlinground.strings.permutations | ||
|
||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Assertions.assertFalse | ||
import kotlin.test.Test | ||
|
||
class CheckPermutationsWithSortingTest { | ||
@Test | ||
fun `should return true with input_str_1=google and input_str_2=ooggle`() { | ||
val inputStr1="google" | ||
val inputStr2="ooggle" | ||
assertTrue { checkPermutationWithSorting(inputStr1, inputStr2) } | ||
} | ||
@Test | ||
fun `should return true with input_str_1=not and input_str_2=top`() { | ||
val inputStr1="not" | ||
val inputStr2="top" | ||
assertFalse { checkPermutationWithSorting(inputStr1, inputStr2) } | ||
} | ||
} | ||
|
||
class CheckPermutationsWithMapTest { | ||
@Test | ||
fun `should return true with input_str_1=google and input_str_2=ooggle`() { | ||
val inputStr1="google" | ||
val inputStr2="ooggle" | ||
assertTrue { checkPermutationWithMap(inputStr1, inputStr2) } | ||
} | ||
@Test | ||
fun `should return true with input_str_1=not and input_str_2=top`() { | ||
val inputStr1="not" | ||
val inputStr2="top" | ||
assertFalse { checkPermutationWithMap(inputStr1, inputStr2) } | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../permutations/GeneratePermutationsTest.kt → .../permutations/GeneratePermutationsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters