Skip to content

Commit

Permalink
palindrome
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonmiranda committed Nov 8, 2024
1 parent 4b3c6ba commit 7e051f9
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/main/java/interview/Palindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package interview;

import java.util.HashMap;
import java.util.Map;

public class Palindrome {

public static void main(String... args) {

System.out.println(numberOfWaysToMakeAPalindrome("abccd"));
}

public static int numberOfWaysToMakeAPalindrome(String s) {

Map<Character, Integer> countMap = new HashMap<>();
var arr = s.toCharArray();

for (char c : arr) {
int count = countMap.getOrDefault(c, 0);
countMap.put(c, ++count);
}

int oddCount = 0;
for (Integer value : countMap.values()) {
if (value % 2 != 0)
oddCount++;
}

if (oddCount == 0)
return 26 + 1;

if (oddCount == 1)
return 1;

if (oddCount == 2)
return 2;


return 0;
}

// if pair
// there should have a pair amount of any character in the string
// odd
// if odd
// only one character present can be of an odd amount

}

0 comments on commit 7e051f9

Please sign in to comment.