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