From 7f075e3af2d36956944a8e50fbcb3035c26d0bb4 Mon Sep 17 00:00:00 2001 From: Amit Date: Tue, 24 Oct 2017 01:08:44 -0500 Subject: [PATCH 1/4] program to check if input string is valid anagram --- Others/validAnagram.java | 89 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Others/validAnagram.java diff --git a/Others/validAnagram.java b/Others/validAnagram.java new file mode 100644 index 000000000000..3c30658ef2f1 --- /dev/null +++ b/Others/validAnagram.java @@ -0,0 +1,89 @@ +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Dungeoun on 10/2/16. + */ + + + +public class Solution { + + public static boolean isAnagram(String s, String t) { + + + HashMap hash1 = new HashMap<>(); + + HashMap hash2 = new HashMap<>(); + + boolean a = false; + + + 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 (int i = 0; i < t.length(); i++) { + + if (hash2.containsKey(t.charAt(i)) == false) { + + hash2.put(t.charAt(i), 1); + + } else { + + hash2.put(t.charAt(i), hash2.get(t.charAt(i)) + 1); + + } + + } + + + if(hash1.size()!=hash2.size()){ + a = false; + } + + else if((hash1.size()==0 && hash2.size()==0) ) { + a = true; + } + else if(hash1.size()!=hash2.size()){ + a = false; + } + + else { + for (Map.Entry entry : hash1.entrySet()) { + + + if (hash2.containsKey(entry.getKey()) == true && hash2.get(entry.getKey()) == entry.getValue()) { + + a = true; + + } + else { + a = false; + break; + } + + } + } + return a; + + } + + public static void main( String []args){ + + boolean b = isAnagram("a","ab"); + + System.out.println(b); + + } +} From 0bc1b912239dc6cfd10155c961389219888c736b Mon Sep 17 00:00:00 2001 From: Amit Date: Tue, 24 Oct 2017 01:13:01 -0500 Subject: [PATCH 2/4] Finds the longest palindrome --- Others/longestPalindrome.java | 64 +++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Others/longestPalindrome.java diff --git a/Others/longestPalindrome.java b/Others/longestPalindrome.java new file mode 100644 index 000000000000..fad080629cd1 --- /dev/null +++ b/Others/longestPalindrome.java @@ -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 hash1 = new HashMap<>(); + + int length =0; + + + for(int i = 0; i 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); + + } +} From 9d849a97f6d5b2ff202dc818e466563c0f514a67 Mon Sep 17 00:00:00 2001 From: Amit Date: Tue, 24 Oct 2017 01:16:45 -0500 Subject: [PATCH 3/4] finds the longest palindrome --- Others/longest_Palindrome.java | 64 ++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Others/longest_Palindrome.java diff --git a/Others/longest_Palindrome.java b/Others/longest_Palindrome.java new file mode 100644 index 000000000000..fad080629cd1 --- /dev/null +++ b/Others/longest_Palindrome.java @@ -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 hash1 = new HashMap<>(); + + int length =0; + + + for(int i = 0; i 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); + + } +} From 03012adfe83cba7024bacf34d1f7b860e6c99a6d Mon Sep 17 00:00:00 2001 From: Amit Date: Tue, 24 Oct 2017 01:17:06 -0500 Subject: [PATCH 4/4] removing old longest palindrome code --- Others/longestPalindrome.java | 64 ----------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 Others/longestPalindrome.java diff --git a/Others/longestPalindrome.java b/Others/longestPalindrome.java deleted file mode 100644 index fad080629cd1..000000000000 --- a/Others/longestPalindrome.java +++ /dev/null @@ -1,64 +0,0 @@ -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 hash1 = new HashMap<>(); - - int length =0; - - - for(int i = 0; i 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); - - } -}