Longest Palindrome length in sub-array with rearrange
Problem statement:
Find the longest even palindrome length of the sub array from the given string of digits. The palindrome can be formed by rearranging the characters as well.
Example:
Given a string "1234535498"
The palindrome formed can be 345543 and the length is 6.
My Solution:
Create a boolean array which will be true if it can form a palindrome. Find the contiguous boolean subarray with maximum number of true values. If the length is odd rerun the same with the subarray.
Comments are welcome to provide a better solution or evaluate this solution.
Algorithm:
- Iterate through the input string character by character.
- For each character, have a counter and increment it for each loop.
- Check if the character is present in the character array and get the last index of it.
- if the last index is not of -1, insert true in the boolean array for the last index and the counter.
- finally insert the character to the character array for the counter.
- After the above steps, we will get the boolean array filled with true for elements which can form palindrome.
- The next step is to find the longest contiguous boolean sub-array in the boolean array.
- Create a boolean array of same length as of the input string.
- Read each character in the string one by one.
- 1234535498 - input string
- Create two new arrays of the length same as input string, first one is character array and the second one is boolean array.
- input 1 234535498 - counter 0
- index of 1 in character [] is -1.
- Skip updating boolean array as index is -1.
- boolean[] - [false, false,false,false,false,false,false,false,false,false]
- insert 1 in the character array.
- character[] - [1, null,null,null,null,null,null,null,null,null]
- input 1 2 34535498 - counter 1
- index of 2 in character [1, null,null,null,null,null,null,null,null,null] is -1.
- Skip updating boolean array as index is -1.
- boolean[] - [false, false,false,false,false,false,false,false,false,false]
- insert 2 in the character array.
- character[] - [1, 2,null,null,null,null,null,null,null,null]
- input 12 3 4535498 - counter 2
- index of 3 in character [1, 2,null,null,null,null,null,null,null,null] is -1.
- Skip updating boolean array as index is -1.
- boolean[] - [false, false,false,false,false,false,false,false,false,false]
- insert 3 in the character array.
- character[] - [1, 2,3,null,null,null,null,null,null,null]
- input 123 4 535498 - counter 3
- index of 4 in character [1, 2,3,null,null,null,null,null,null,null] is -1.
- Skip updating boolean array as index is -1.
- boolean[] - [false, false,false,false,false,false,false,false,false,false]
- insert 4 in the character array.
- character[] - [1, 2,3,4,null,null,null,null,null,null]
- input 1234 5 35498 - counter 4
- index of 5 in character [1, 2,3,4,null,null,null,null,null,null] is -1.
- Skip updating boolean array as index is -1.
- boolean[] - [false, false,false,false,false,false,false,false,false,false]
- insert 5 in the character array.
- character[] - [1, 2,3,4,5,null,null,null,null,null]
- input 12345 3 5498 - counter 5
- index of 3 in character [1, 2,3,4,5,null,null,null,null,null] is 2.
- Make the 2nd (index) element and 5th (counter) element as true.
- boolean[] - [false, false,true,false,false,true,false,false,false,false]
- insert 3 in the character array.
- character[] - [1, 2,3,4,5,3,null,null,null,null]
- input 123453 5 498 - counter 6
- index of 5 in character [1, 2,3,4,5,3,null,null,null,null] is 4.
- Make the 4th (index) element and 6th (counter) element as true.
- boolean[] - [false, false,true,false,true,true,true,false,false,false]
- insert 5 in the character array.
- character[] - [1, 2,3,4,5,3,5,null,null,null]
- input 1234535 4 98 - counter 7
- index of 4 in character [1, 2,3,4,5,3,5,null,null,null] is 3.
- Make the 3rd (index) element and 7th (counter) element as true.
- boolean[] - [false, false,true,true,true,true,true,true,false,false]
- insert 4 in the character array.
- character[] - [1, 2,3,4,5,3,5,4,null,null]
- input 12345354 9 8 - counter 8
- index of 9 in character [1, 2,3,4,5,3,5,4,null,null] is -1.
- Skip updating boolean array as index is -1.
- boolean[] - [false, false,true,true,true,true,true,true,false,false]
- insert 9 in the character array.
- character[] - [1, 2,3,4,5,3,5,4,9,null]
- input 123453549 8 - counter 9
- index of 8 in character [1, 2,3,4,5,3,5,4,9,null] is -1.
- Skip updating boolean array as index is -1.
- boolean[] - [false, false,true,true,true,true,true,true,false,false]
- insert 8 in the character array.
- character[] - [1, 2,3,4,5,3,5,4,9,8]
Now find the longest contiguous boolean sub array in the boolean array. In our case it is 6. 2nd to 7th element in the boolean array.
Comments
Post a Comment