Skip to content

Commit

Permalink
feat(strings): check permutations
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLusina committed Dec 27, 2024
1 parent f4c8333 commit 90bf1a9
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 11 deletions.
4 changes: 2 additions & 2 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ repositories {
}

dependencies {
implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.9.1")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.10")
implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.23.7")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:2.1.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object Acronym {
val acronym = StringBuilder()

// for each word
words.forEach { acronym.append(it[0].toUpperCase()) }
words.forEach { acronym.append(it[0].uppercaseChar()) }

return acronym.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ object Pangrams {
return false
var i = 0
sentence.toCharArray()
.map { it.toUpperCase().toInt() }
.map { it.uppercaseChar().toInt() }
.filter { it.toChar() in 'A'..'Z' }
.forEach { i = i or (1 shl it.toChar() - 'A') }

return i == i or (1 shl ('Z') + 1 - 'A') - 1
}

val alphaLength = 26
fun isPangramX(input: String) = input.toLowerCase().replace(Regex("[^a-z]"), "").toSet().size == alphaLength
fun isPangramX(input: String) = input.lowercase().replace(Regex("[^a-z]"), "").toSet().size == alphaLength

}

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Generate All Permutations

Given a string of unique letters, find all of its distinct permutations.
# Permutations

Permutation means arranging things with an order. For example, permutations of [1, 2] are [1, 2] and [2, 1].

## Generate All Permutations

Given a string of unique letters, find all of its distinct permutations.

Input

- letters: a string of unique letters
Expand All @@ -19,3 +21,16 @@ Input:

1. letters = abc
- Output: abc acb bac bca cab cba

---
## Check Permutation

Determine if a given string is a permutation of another string. Given two strings, write a function to determine if one is a permutation of the other.

### Tags

- Strings

## Tags

- Permutations
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
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kotlinground.algorithms.backtracking.permutations
package com.kotlinground.strings.permutations

import java.util.stream.Collectors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object WordCount {
* @return [Map] with keys with words and values as the counts of numbers
* */
fun phrase(sentence: String): Map<String, Int> {
val phrase = sentence.toLowerCase().replace(Regex("[^\\w']"), " ").trim()
val phrase = sentence.lowercase().replace(Regex("[^\\w']"), " ").trim()
val words = phrase.split(Regex("\\s+"))
val groupedWords = words.groupBy({ w -> w })

Expand Down
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) }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kotlinground.algorithms.backtracking.permutations
package com.kotlinground.strings.permutations

import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
Expand Down

0 comments on commit 90bf1a9

Please sign in to comment.