-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b3c6ba
commit 7e051f9
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |