Skip to content

Commit 6efca54

Browse files
committed
Added tests
1 parent 99be171 commit 6efca54

File tree

2 files changed

+298
-1
lines changed
  • src
    • main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target
    • test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target

2 files changed

+298
-1
lines changed

src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_grea
44
// #2025_11_05_Time_4_ms_(100.00%)_Space_46.12_MB_(77.78%)
55

66
class Solution {
7-
private fun func(i: Int, target: String, ans: CharArray, l: Int, r: Int, freq: IntArray, end: Boolean): Boolean {
7+
internal fun func(i: Int, target: String, ans: CharArray, l: Int, r: Int, freq: IntArray, end: Boolean): Boolean {
88
if (l > r) {
99
return String(ans).compareTo(target) > 0
1010
}

src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_grea
22

33
import org.hamcrest.CoreMatchers.equalTo
44
import org.hamcrest.MatcherAssert.assertThat
5+
import org.hamcrest.Matchers.allOf
6+
import org.hamcrest.Matchers.anyOf
7+
import org.hamcrest.Matchers.containsString
8+
import org.hamcrest.Matchers.greaterThanOrEqualTo
9+
import org.hamcrest.Matchers.hasLength
10+
import org.hamcrest.Matchers.not
11+
import org.hamcrest.Matchers.nullValue
512
import org.junit.jupiter.api.Test
613

714
internal class SolutionTest {
@@ -36,4 +43,294 @@ internal class SolutionTest {
3643
equalTo<String>("aca"),
3744
)
3845
}
46+
47+
@Test
48+
fun lexPalindromicPermutation5() {
49+
// Branch: oddc > 1
50+
val result = Solution().lexPalindromicPermutation("abc", "a")
51+
assertThat<String>(result, equalTo<String>(""))
52+
}
53+
54+
@Test
55+
fun lexPalindromicPermutation6() {
56+
// Branch: oddc = 1
57+
val result = Solution().lexPalindromicPermutation("aab", "a")
58+
assertThat<String>(
59+
result,
60+
allOf<String>(not<String>(equalTo<String>("")), hasLength(3)),
61+
)
62+
}
63+
64+
@Test
65+
fun lexPalindromicPermutation7() {
66+
// Branch: oddc = 0
67+
val result = Solution().lexPalindromicPermutation("aabb", "ab")
68+
assertThat<String>(result, not<String>(equalTo<String>("")))
69+
}
70+
71+
@Test
72+
fun lexPalindromicPermutation8() {
73+
// Branch: func returns false
74+
val result = Solution().lexPalindromicPermutation("abc", "xyz")
75+
assertThat<String>(result, equalTo<String>(""))
76+
}
77+
78+
@Test
79+
fun lexPalindromicPermutation9() {
80+
// Edge case: length = 1
81+
val result = Solution().lexPalindromicPermutation("a", "a")
82+
assertThat<String>(result, equalTo<String>(""))
83+
}
84+
85+
@Test
86+
fun lexPalindromicPermutation10() {
87+
// Branch: l > r and comparison result > 0
88+
val target = "a"
89+
val freq = IntArray(26)
90+
val ans = charArrayOf('b', 'b')
91+
92+
val result = Solution().func(0, target, ans, 1, 0, freq, false)
93+
assertThat<Boolean>(result, equalTo<Boolean>(true))
94+
}
95+
96+
@Test
97+
fun lexPalindromicPermutation11() {
98+
// Branch: l > r and comparison result <= 0
99+
val target = "z"
100+
val freq = IntArray(26)
101+
val ans = charArrayOf('a', 'a')
102+
103+
val result = Solution().func(0, target, ans, 1, 0, freq, false)
104+
assertThat<Boolean>(result, equalTo<Boolean>(false))
105+
}
106+
107+
@Test
108+
fun lexPalindromicPermutation12() {
109+
// Branch: l == r with available character
110+
val target = "a"
111+
val freq = IntArray(26)
112+
// 'a' has frequency 1
113+
freq[0] = 1
114+
val ans = CharArray(1)
115+
116+
val result = Solution().func(0, target, ans, 0, 0, freq, false)
117+
assertThat<Boolean>(result, equalTo<Boolean>(false))
118+
assertThat<Char>(ans[0], equalTo<Char>('#'))
119+
}
120+
121+
@Test
122+
fun lexPalindromicPermutation13() {
123+
// Branch: end = true, finds char with freq > 1
124+
val target = "ab"
125+
val freq = IntArray(26)
126+
// 'a' can form a pair
127+
freq[0] = 2
128+
freq[1] = 0
129+
val ans = CharArray(2)
130+
131+
val result = Solution().func(1, target, ans, 0, 1, freq, true)
132+
assertThat<Boolean>(
133+
result,
134+
anyOf<Boolean>(equalTo<Boolean>(true), equalTo<Boolean>(false)),
135+
)
136+
// Frequency restored
137+
assertThat<Int>(freq[0], equalTo<Int>(2))
138+
}
139+
140+
@Test
141+
fun lexPalindromicPermutation14() {
142+
// Branch: end = true, no char has freq > 1
143+
val target = "ab"
144+
val freq = IntArray(26)
145+
freq[0] = 1
146+
freq[1] = 1
147+
val ans = CharArray(2)
148+
149+
val result = Solution().func(1, target, ans, 0, 1, freq, true)
150+
assertThat<Boolean>(result, equalTo<Boolean>(false))
151+
}
152+
153+
@Test
154+
fun lexPalindromicPermutation15() {
155+
// Branch: end = true, tries different pairs
156+
val target = "abc"
157+
val freq = IntArray(26)
158+
freq[0] = 2
159+
freq[1] = 2
160+
freq[2] = 2
161+
val ans = CharArray(3)
162+
163+
Solution().func(1, target, ans, 0, 2, freq, true)
164+
assertThat<Int>(freq[0], equalTo<Int>(0))
165+
assertThat<Int>(freq[1], equalTo<Int>(2))
166+
assertThat<Int>(freq[2], equalTo<Int>(1))
167+
}
168+
169+
@Test
170+
fun lexPalindromicPermutation16() {
171+
// Branch: end = false, curr has freq > 1
172+
val target = "a"
173+
val freq = IntArray(26)
174+
freq[0] = 2
175+
val ans = CharArray(2)
176+
177+
Solution().func(0, target, ans, 0, 1, freq, false)
178+
assertThat<Int>(freq[0], equalTo<Int>(0))
179+
}
180+
181+
@Test
182+
fun lexPalindromicPermutation17() {
183+
// Branch: end = false, curr has freq <= 1
184+
val target = "a"
185+
val freq = IntArray(26)
186+
freq[0] = 0
187+
val ans = CharArray(2)
188+
189+
Solution().func(0, target, ans, 0, 1, freq, false)
190+
assertThat<Int>(freq[0], equalTo<Int>(0))
191+
}
192+
193+
@Test
194+
fun lexPalindromicPermutation18() {
195+
// Branch: end = false, finds next char with freq > 1
196+
val target = "a"
197+
val freq = IntArray(26)
198+
freq[0] = 0
199+
freq[1] = 2
200+
val ans = CharArray(2)
201+
202+
Solution().func(0, target, ans, 0, 1, freq, false)
203+
assertThat<Int>(freq[0], equalTo<Int>(0))
204+
assertThat<Int>(freq[1], equalTo<Int>(0))
205+
}
206+
207+
@Test
208+
fun lexPalindromicPermutation19() {
209+
// Branch: end = false, no next char with freq > 1
210+
val target = "z"
211+
val freq = IntArray(26)
212+
freq[25] = 1
213+
val ans = CharArray(2)
214+
215+
val result = Solution().func(0, target, ans, 0, 1, freq, false)
216+
assertThat<Boolean>(result, equalTo<Boolean>(false))
217+
}
218+
219+
@Test
220+
fun lexPalindromicPermutation20() {
221+
// Branch: end = false transitions to end = true
222+
val target = "ab"
223+
val freq = IntArray(26)
224+
freq[0] = 2
225+
freq[1] = 2
226+
val ans = CharArray(2)
227+
228+
Solution().func(0, target, ans, 0, 1, freq, false)
229+
230+
assertThat<Int>(freq[0], equalTo<Int>(2))
231+
assertThat<Int>(freq[1], equalTo<Int>(0))
232+
}
233+
234+
@Test
235+
fun lexPalindromicPermutation21() {
236+
// Verify result is always a palindrome
237+
val result = Solution().lexPalindromicPermutation("aabbcc", "abc")
238+
if (!result.isEmpty()) {
239+
val reversed = StringBuilder(result).reverse().toString()
240+
assertThat<String>(result, equalTo<String>(reversed))
241+
}
242+
}
243+
244+
@Test
245+
fun lexPalindromicPermutation22() {
246+
// Verify character frequencies are preserved
247+
val input = "aabbcc"
248+
val result = Solution().lexPalindromicPermutation(input, "abc")
249+
250+
if (!result.isEmpty()) {
251+
val inputFreq = IntArray(26)
252+
val resultFreq = IntArray(26)
253+
254+
for (c in input.toCharArray()) {
255+
inputFreq[c.code - 'a'.code]++
256+
}
257+
for (c in result.toCharArray()) {
258+
resultFreq[c.code - 'a'.code]++
259+
}
260+
261+
assertThat<IntArray>(resultFreq, equalTo<IntArray>(inputFreq))
262+
}
263+
}
264+
265+
@Test
266+
fun lexPalindromicPermutation23() {
267+
// Result length should match input length
268+
val input = "aabbccdd"
269+
val result = Solution().lexPalindromicPermutation(input, "abcd")
270+
271+
if (!result.isEmpty()) {
272+
assertThat<Int>(result.length, equalTo<Int>(input.length))
273+
}
274+
}
275+
276+
@Test
277+
fun lexPalindromicPermutation24() {
278+
// Result should be >= target in lexicographical order
279+
val result = Solution().lexPalindromicPermutation("aabbcc", "abc")
280+
281+
if (!result.isEmpty()) {
282+
assertThat<Int>(result.compareTo("abc"), greaterThanOrEqualTo<Int>(0))
283+
}
284+
}
285+
286+
@Test
287+
fun lexPalindromicPermutation25() {
288+
// Complex scenario with multiple characters
289+
val result = Solution().lexPalindromicPermutation("aabbccdd", "abcd")
290+
291+
assertThat<String>(
292+
result,
293+
anyOf<String>(
294+
equalTo<String>(""),
295+
allOf<String>(
296+
hasLength(8),
297+
containsString("a"),
298+
containsString("b"),
299+
),
300+
),
301+
)
302+
}
303+
304+
@Test
305+
fun lexPalindromicPermutation26() {
306+
// Edge case: empty string (if applicable)
307+
val result = Solution().lexPalindromicPermutation("", "")
308+
assertThat<String>(
309+
result,
310+
anyOf<String>(equalTo<String>(""), not<Any>(nullValue())),
311+
)
312+
}
313+
314+
@Test
315+
fun lexPalindromicPermutation27() {
316+
// Verify frequency array is restored after recursion
317+
val target = "aabb"
318+
val freq = IntArray(26)
319+
val freqCopy: IntArray = freq.clone()
320+
321+
val ans = CharArray(4)
322+
Solution().func(0, target, ans, 0, 3, freq, false)
323+
324+
assertThat<IntArray>(freq, equalTo<IntArray>(freqCopy))
325+
}
326+
327+
@Test
328+
fun lexPalindromicPermutation28() {
329+
// Verify char array is properly initialized
330+
val result = Solution().lexPalindromicPermutation("aa", "a")
331+
332+
if (!result.isEmpty()) {
333+
assertThat<String>(result, not<String>(containsString("#")))
334+
}
335+
}
39336
}

0 commit comments

Comments
 (0)