Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Finds the longest palindrome
  • Loading branch information
amit-dat committed Oct 24, 2017
commit 0bc1b912239dc6cfd10155c961389219888c736b
64 changes: 64 additions & 0 deletions Others/longestPalindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.util.HashMap;
import java.util.Map;

/**
* Created by Dungeoun on 10/2/16.
*/
public class Solution {

public static int longestPalindrome(String s) {


HashMap<Character, Integer> hash1 = new HashMap<>();

int length =0;


for(int i = 0; i<s.length(); i++){

if(hash1.containsKey(s.charAt(i))==false){

hash1.put(s.charAt(i), 1);

}

else{

hash1.put(s.charAt(i), hash1.get(s.charAt(i))+1);

}
}

for(Map.Entry<Character,Integer> entry: hash1.entrySet()){

if(entry.getValue()%2 == 0){

length = length + entry.getValue();

}
else if(entry.getValue()%2 != 0){

length = length + entry.getValue()-1;

}


}

if(s.length()>length) {

return length + 1;
}
else return length;

}


public static void main(String []args){

int l = longestPalindrome("bb");

System.out.println(l);

}
}