Skip to content

program to check if input string is valid anagram#260

Closed
amit-dat wants to merge 4 commits into
TheAlgorithms:masterfrom
amit-dat:master
Closed

program to check if input string is valid anagram#260
amit-dat wants to merge 4 commits into
TheAlgorithms:masterfrom
amit-dat:master

Conversation

@amit-dat

Copy link
Copy Markdown

No description provided.

Comment thread Others/longestPalindrome.java Outdated
@@ -1,64 +0,0 @@
import java.util.HashMap;
import java.util.Map;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please name your class according to the filename.

Comment thread Others/validAnagram.java
@@ -0,0 +1,89 @@
import java.util.HashMap;
import java.util.Map;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please name your file according to the class name.

Comment thread Others/validAnagram.java
else if((hash1.size()==0 && hash2.size()==0) ) {
a = true;
}
else if(hash1.size()!=hash2.size()){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary. You has check this condition above, too.

Comment thread Others/validAnagram.java


if(hash1.size()!=hash2.size()){
a = false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can in this cases write return false;

Comment thread Others/validAnagram.java
}

else if((hash1.size()==0 && hash2.size()==0) ) {
a = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing as above. return true;

Comment thread Others/validAnagram.java

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of == false use the not-operator !

Comment thread Others/validAnagram.java

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

if (hash2.containsKey(t.charAt(i)) == false) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of == false use the not-operator !

Comment thread Others/validAnagram.java
}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this monster. You can use a = hash1.equals(hash2); For checking the equality of the HashMaps.

Comment thread Others/validAnagram.java

public static void main( String []args){

boolean b = isAnagram("a","ab");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b is unnecessary. Because you can send the return value of the method right away to the println method.

Comment thread Others/validAnagram.java
public class Solution {

public static boolean isAnagram(String s, String t) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary empty lines.

Comment thread Others/validAnagram.java
HashMap<Character, Integer> hash2 = new HashMap<>();

boolean a = false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put in some comments in your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants