Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
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
64 changes: 64 additions & 0 deletions 64 Others/longest_Palindrome.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);

}
}
89 changes: 89 additions & 0 deletions 89 Others/validAnagram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import java.util.HashMap;
import java.util.Map;

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.

/**
* Created by Dungeoun on 10/2/16.
*/



public class Solution {

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

Choose a reason for hiding this comment

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

Remove unnecessary empty lines.


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

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

boolean a = false;

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.


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

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

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 !


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) {

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 !


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;

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;

}

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

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;

}
else if(hash1.size()!=hash2.size()){

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.

a = false;
}

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

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.



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");

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.


System.out.println(b);

}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.