Skip to content

Commit c1f59c4

Browse files
Merge pull request #170
workBranch
2 parents 191d684 + 6a0223f commit c1f59c4

File tree

6 files changed

+165
-5
lines changed

6 files changed

+165
-5
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.github.contest
22

33

4+
import com.github.contest.bitManipulation.subsetXORSum
45
import com.github.contest.strings.shiftingLetters
6+
import com.github.contest.strings.smallestStringProdVariant
57

68
import java.util.TreeMap
79

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

1315
fun main() {
1416

15-
shiftingLetters("abc", intArrayOf(3, 5, 9)).also { println(it) }
17+
subsetXORSum(intArrayOf(5, 1, 6))
1618
}
1719

1820
fun testing() {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
11
package com.github.contest.bitManipulation
22

3+
/**
4+
* 1863. Sum of All Subset XOR Totals
5+
*/
6+
7+
fun subsetXORSum(nums: IntArray): Int {
8+
var totalXORSum = 0
9+
10+
fun calculateSubsetXOR(index: Int, currentXOR: Int) {
11+
if (index == nums.size) {
12+
totalXORSum += currentXOR
13+
return
14+
}
15+
16+
// Include the current element
17+
calculateSubsetXOR(index + 1, currentXOR xor nums[index])
18+
19+
// Exclude the current element
20+
calculateSubsetXOR(index + 1, currentXOR)
21+
}
22+
23+
calculateSubsetXOR(0, 0)
24+
return totalXORSum
25+
}

contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,19 @@ fun findMissingAndRepeatedValues(grid: Array<IntArray>): IntArray {
256256
}
257257

258258
return res
259+
}
260+
261+
/**
262+
* 2351. First Letter to Appear Twice
263+
*/
264+
265+
fun repeatedCharacter(s: String): Char {
266+
val alphabet = IntArray(26)
267+
for (char in s) {
268+
val index = char - 'a'
269+
if (alphabet[index] == 0) alphabet[index] += 1
270+
else return char
271+
}
272+
273+
return 'a'
259274
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package com.github.contest.strings
2+

contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ package com.github.contest.strings
55
*/
66

77
fun shiftingLetters(str: String, shifts: IntArray): String {
8-
val shifting = IntArray(shifts.size)
9-
var sum = shifts.sum()
8+
val shifting = LongArray(shifts.size)
9+
var sum = 0L
1010
var res = ""
1111

12+
for (element in shifts) {
13+
val num = element.toLong()
14+
sum += num
15+
}
16+
1217
for (i in shifting.indices) {
1318
shifting[i] = sum
14-
sum -= shifts[i]
19+
sum -= shifts[i].toLong()
1520
}
1621

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

26-
private fun shiftLetter(char: Char, shift: Int): Char = when {
31+
private fun shiftLetter(char: Char, shift: Long): Char = when {
2732
char in 'a'..'z' -> {
2833
val base = 'a'.code
2934
val offset = char.code - base
3035
val shifted = (offset + shift) % 26
3136
(base + shifted).toChar()
3237
}
38+
3339
else -> char
3440
}
3541

42+
/**
43+
* 2734. Lexicographically Smallest String After Substring Operation
44+
*/
45+
46+
fun smallestString(s: String): String {
47+
if (s.hasSingle()) return when {
48+
s[0] == 'a' -> 'z'.toString()
49+
else -> Char(s[0].code - 1).toString()
50+
}
51+
val chars = s.toCharArray()
52+
val n = chars.size
53+
var start = -1
54+
55+
for (i in 0 until n) {
56+
if (chars[i] != 'a') {
57+
start = i
58+
break
59+
}
60+
}
61+
62+
if (start == -1) {
63+
chars[n - 1] = 'z'
64+
return String(chars)
65+
}
66+
67+
for (i in start until n) {
68+
if (chars[i] != 'a') chars[i]--
69+
else break
70+
}
71+
72+
return String(chars)
73+
74+
}
75+
76+
fun String.hasSingle(): Boolean = when {
77+
this.length == 1 -> true
78+
else -> false
79+
}
80+
81+
/**
82+
* 2278. Percentage of Letter in String
83+
*/
84+
85+
fun percentageLetter(s: String, letter: Char): Int {
86+
var count = 0
87+
for (char in s) if (char == letter) count++
88+
return if (count == 0) 0 else (count * 100) / s.length
89+
}
90+
91+
92+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.github.contest.strings
2+
3+
/**
4+
* 848. Shifting Letters
5+
* Prod Variant
6+
*/
7+
8+
fun shiftingLetterProdVariant(str: String, shifts: IntArray): String {
9+
var sum = shifts.toLongArray().sum()
10+
return buildString {
11+
str.forEachIndexed { index, letter ->
12+
val new = letter.shiftLetter(sum)
13+
append(new)
14+
sum -= shifts[index]
15+
}
16+
}
17+
}
18+
19+
private fun IntArray.toLongArray(): LongArray {
20+
val new = LongArray(this.size)
21+
for (i in indices) {
22+
new[i] = this[i].toLong()
23+
}
24+
25+
return new
26+
}
27+
28+
private fun Char.shiftLetter(shift: Long): Char = when {
29+
this in 'a'..'z' -> {
30+
val base = 'a'.code
31+
val offset = this.code - base
32+
val shifted = (offset + shift) % 26
33+
(base + shifted).toChar()
34+
}
35+
36+
else -> this
37+
}
38+
39+
/**
40+
* 2734. Lexicographically Smallest String After Substring Operation
41+
* Prod Variant
42+
*/
43+
44+
fun smallestStringProdVariant(s: String): String {
45+
val firstNonAIndex = s.indexOfFirst { it != 'a' }
46+
47+
return if (firstNonAIndex == -1) {
48+
s.dropLast(1) + 'z'
49+
} else {
50+
val modifiedChars = s.toCharArray().also { chars ->
51+
for (i in firstNonAIndex until chars.size) {
52+
if (chars[i] != 'a') {
53+
chars[i]--
54+
} else {
55+
break
56+
}
57+
}
58+
}
59+
String(modifiedChars)
60+
}
61+
}

0 commit comments

Comments
 (0)