Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.contest


import com.github.contest.bitManipulation.subsetXORSum
import com.github.contest.strings.shiftingLetters
import com.github.contest.strings.smallestStringProdVariant

import java.util.TreeMap

Expand All @@ -12,7 +14,7 @@ import java.util.TreeMap

fun main() {

shiftingLetters("abc", intArrayOf(3, 5, 9)).also { println(it) }
subsetXORSum(intArrayOf(5, 1, 6))
}

fun testing() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
package com.github.contest.bitManipulation

/**
* 1863. Sum of All Subset XOR Totals
*/

fun subsetXORSum(nums: IntArray): Int {
var totalXORSum = 0

fun calculateSubsetXOR(index: Int, currentXOR: Int) {
if (index == nums.size) {
totalXORSum += currentXOR
return
}

// Include the current element
calculateSubsetXOR(index + 1, currentXOR xor nums[index])

// Exclude the current element
calculateSubsetXOR(index + 1, currentXOR)
}

calculateSubsetXOR(0, 0)
return totalXORSum
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,19 @@ fun findMissingAndRepeatedValues(grid: Array<IntArray>): IntArray {
}

return res
}

/**
* 2351. First Letter to Appear Twice
*/

fun repeatedCharacter(s: String): Char {
val alphabet = IntArray(26)
for (char in s) {
val index = char - 'a'
if (alphabet[index] == 0) alphabet[index] += 1
else return char
}

return 'a'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.github.contest.strings

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ package com.github.contest.strings
*/

fun shiftingLetters(str: String, shifts: IntArray): String {
val shifting = IntArray(shifts.size)
var sum = shifts.sum()
val shifting = LongArray(shifts.size)
var sum = 0L
var res = ""

for (element in shifts) {
val num = element.toLong()
sum += num
}

for (i in shifting.indices) {
shifting[i] = sum
sum -= shifts[i]
sum -= shifts[i].toLong()
}

for (i in str.indices) {
Expand All @@ -23,13 +28,65 @@ fun shiftingLetters(str: String, shifts: IntArray): String {
return res
}

private fun shiftLetter(char: Char, shift: Int): Char = when {
private fun shiftLetter(char: Char, shift: Long): Char = when {
char in 'a'..'z' -> {
val base = 'a'.code
val offset = char.code - base
val shifted = (offset + shift) % 26
(base + shifted).toChar()
}

else -> char
}

/**
* 2734. Lexicographically Smallest String After Substring Operation
*/

fun smallestString(s: String): String {
if (s.hasSingle()) return when {
s[0] == 'a' -> 'z'.toString()
else -> Char(s[0].code - 1).toString()
}
val chars = s.toCharArray()
val n = chars.size
var start = -1

for (i in 0 until n) {
if (chars[i] != 'a') {
start = i
break
}
}

if (start == -1) {
chars[n - 1] = 'z'
return String(chars)
}

for (i in start until n) {
if (chars[i] != 'a') chars[i]--
else break
}

return String(chars)

}

fun String.hasSingle(): Boolean = when {
this.length == 1 -> true
else -> false
}

/**
* 2278. Percentage of Letter in String
*/

fun percentageLetter(s: String, letter: Char): Int {
var count = 0
for (char in s) if (char == letter) count++
return if (count == 0) 0 else (count * 100) / s.length
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.github.contest.strings

/**
* 848. Shifting Letters
* Prod Variant
*/

fun shiftingLetterProdVariant(str: String, shifts: IntArray): String {
var sum = shifts.toLongArray().sum()
return buildString {
str.forEachIndexed { index, letter ->
val new = letter.shiftLetter(sum)
append(new)
sum -= shifts[index]
}
}
}

private fun IntArray.toLongArray(): LongArray {
val new = LongArray(this.size)
for (i in indices) {
new[i] = this[i].toLong()
}

return new
}

private fun Char.shiftLetter(shift: Long): Char = when {
this in 'a'..'z' -> {
val base = 'a'.code
val offset = this.code - base
val shifted = (offset + shift) % 26
(base + shifted).toChar()
}

else -> this
}

/**
* 2734. Lexicographically Smallest String After Substring Operation
* Prod Variant
*/

fun smallestStringProdVariant(s: String): String {
val firstNonAIndex = s.indexOfFirst { it != 'a' }

return if (firstNonAIndex == -1) {
s.dropLast(1) + 'z'
} else {
val modifiedChars = s.toCharArray().also { chars ->
for (i in firstNonAIndex until chars.size) {
if (chars[i] != 'a') {
chars[i]--
} else {
break
}
}
}
String(modifiedChars)
}
}