|
| 1 | +## Longest substring to form a Palindrome |
| 2 | +Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. [🔗Goto](https://practice.geeksforgeeks.org/problems/longest-substring-whose-character-rearranged-can-form-a-palindrome/1#) |
| 3 | + |
| 4 | +```Example 1: |
| 5 | +
|
| 6 | +Input: |
| 7 | +S = "aabe" |
| 8 | +Output: |
| 9 | +3 |
| 10 | +Explanation: |
| 11 | +The substring "aab" can be rearranged to |
| 12 | +"aba" which is the longest palindrome |
| 13 | +possible for this String. |
| 14 | +
|
| 15 | +Example 2: |
| 16 | +Input: |
| 17 | +S = "adbabd" |
| 18 | +Output: |
| 19 | +6 |
| 20 | +Explanation: |
| 21 | +The whole string “adbabd” can be |
| 22 | +rearranged to form a palindromic substring. |
| 23 | +One possible arrangement is "abddba". |
| 24 | +Thus, output length of the string is 6. |
| 25 | +
|
| 26 | +``` |
| 27 | +<details> |
| 28 | +<summary>Full Code</summary> |
| 29 | + |
| 30 | +```java |
| 31 | +import java.io.*; |
| 32 | +import java.util.*; |
| 33 | + |
| 34 | +class GFG { |
| 35 | + public static void main(String args[]) throws IOException { |
| 36 | + BufferedReader read = |
| 37 | + new BufferedReader(new InputStreamReader(System.in)); |
| 38 | + int t = Integer.parseInt(read.readLine()); |
| 39 | + while (t-- > 0) { |
| 40 | + String S = read.readLine(); |
| 41 | + |
| 42 | + Solution ob = new Solution(); |
| 43 | + System.out.println(ob.longestSubstring(S)); |
| 44 | + } |
| 45 | + } |
| 46 | +}// } Driver Code Ends |
| 47 | + |
| 48 | + |
| 49 | +//User function Template for Java |
| 50 | + |
| 51 | +class Solution { |
| 52 | + static int longestSubstring(String s) { |
| 53 | + HashMap<Integer, Integer> index = new HashMap<>(); |
| 54 | + |
| 55 | + int answer = 0; |
| 56 | + |
| 57 | + int mask = 0; |
| 58 | + index.put(mask, -1); |
| 59 | + |
| 60 | + for (int i = 0; i < s.length(); i++) { |
| 61 | + |
| 62 | + int temp = (int) s.charAt(i) - 'a'; |
| 63 | + |
| 64 | + mask ^= (1 << temp); |
| 65 | + |
| 66 | + if (index.containsKey(mask)) { |
| 67 | + answer = Math.max(answer, i - index.get(mask)); |
| 68 | + } else { |
| 69 | + index.put(mask, i); |
| 70 | + } |
| 71 | + |
| 72 | + for (int j = 0; j < 26; j++) { |
| 73 | + int mask2 = mask ^ (1 << j); |
| 74 | + if (index.containsKey(mask2)) { |
| 75 | + answer = Math.max(answer, i - index.get(mask2)); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return answer; |
| 80 | + } |
| 81 | +}; |
| 82 | +``` |
| 83 | +</details> |
| 84 | + |
| 85 | +```java |
| 86 | +class Solution { |
| 87 | + static int longestSubstring(String s) { |
| 88 | + HashMap<Integer, Integer> index = new HashMap<>(); |
| 89 | + |
| 90 | + int answer = 0; |
| 91 | + |
| 92 | + int mask = 0; |
| 93 | + index.put(mask, -1); |
| 94 | + |
| 95 | + for (int i = 0; i < s.length(); i++) { |
| 96 | + |
| 97 | + int temp = (int) s.charAt(i) - 'a'; |
| 98 | + |
| 99 | + mask ^= (1 << temp); |
| 100 | + |
| 101 | + if (index.containsKey(mask)) { |
| 102 | + answer = Math.max(answer, i - index.get(mask)); |
| 103 | + } else { |
| 104 | + index.put(mask, i); |
| 105 | + } |
| 106 | + |
| 107 | + for (int j = 0; j < 26; j++) { |
| 108 | + int mask2 = mask ^ (1 << j); |
| 109 | + if (index.containsKey(mask2)) { |
| 110 | + answer = Math.max(answer, i - index.get(mask2)); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return answer; |
| 115 | + } |
| 116 | +}; |
| 117 | +``` |
0 commit comments