program to check if input string is valid anagram#260
program to check if input string is valid anagram#260amit-dat wants to merge 4 commits intoTheAlgorithms:masterfrom
Conversation
Others/longestPalindrome.java
Outdated
| @@ -1,64 +0,0 @@ | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
|
|
|||
There was a problem hiding this comment.
Please name your class according to the filename.
| @@ -0,0 +1,89 @@ | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
|
|
|||
There was a problem hiding this comment.
Please name your file according to the class name.
| else if((hash1.size()==0 && hash2.size()==0) ) { | ||
| a = true; | ||
| } | ||
| else if(hash1.size()!=hash2.size()){ |
There was a problem hiding this comment.
unnecessary. You has check this condition above, too.
|
|
||
|
|
||
| if(hash1.size()!=hash2.size()){ | ||
| a = false; |
There was a problem hiding this comment.
You can in this cases write return false;
| } | ||
|
|
||
| else if((hash1.size()==0 && hash2.size()==0) ) { | ||
| a = true; |
There was a problem hiding this comment.
Same thing as above. return true;
|
|
||
| for (int i = 0; i < s.length(); i++) { | ||
|
|
||
| if (hash1.containsKey(s.charAt(i)) == false) { |
There was a problem hiding this comment.
Instead of == false use the not-operator !
|
|
||
| for (int i = 0; i < t.length(); i++) { | ||
|
|
||
| if (hash2.containsKey(t.charAt(i)) == false) { |
There was a problem hiding this comment.
Instead of == false use the not-operator !
| } | ||
|
|
||
| else { | ||
| for (Map.Entry<Character, Integer> entry : hash1.entrySet()) { |
There was a problem hiding this comment.
Instead of this monster. You can use a = hash1.equals(hash2); For checking the equality of the HashMaps.
|
|
||
| public static void main( String []args){ | ||
|
|
||
| boolean b = isAnagram("a","ab"); |
There was a problem hiding this comment.
b is unnecessary. Because you can send the return value of the method right away to the println method.
| public class Solution { | ||
|
|
||
| public static boolean isAnagram(String s, String t) { | ||
|
|
There was a problem hiding this comment.
Remove unnecessary empty lines.
| HashMap<Character, Integer> hash2 = new HashMap<>(); | ||
|
|
||
| boolean a = false; | ||
|
|
There was a problem hiding this comment.
Put in some comments in your code.
No description provided.