Anagram checking implementation
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
Given two strings, check whether two given strings are anagram of each
other or not. An anagram of a string is another string that contains
same characters, only the order of characters can be different. For
example, âÂÂactâ and âÂÂtacâ are anagram of each other.
Input:
The first line of input contains an integer T denoting the number of
test cases. Each test case consist of two strings in 'lowercase' only,
in a separate line.
Output:
Print "YES" without quotes if the two strings are anagram else print
"NO".
Constraints:
1 ⤠T ⤠30
1 ⤠|s| ⤠100
Example:
Input:
2
geeksforgeeks
forgeeksgeeks
allergy
allergic
Output:
YES
NO
My approach:
/*package whatever //do not write package name here */
import java.util.Scanner;
import java.io.IOException;
import java.util.HashMap;
class GFG
private static String isAnagram (String str1, String str2)
HashMap <Character, Integer>occurs = new HashMap<>();
if (str1.length() != str2.length())
return "NO";
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
for (char ch: str2.toCharArray())
if (!(occurs.containsKey(ch)))
return "NO";
else
int count = occurs.get(ch);
count = count - 1;
if (count < 0)
return "NO";
else
occurs.put(ch,count);
return "YES";
public static void main (String args) throws IOException
Scanner sc = new Scanner (System.in);
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++)
String str1 = sc.next();
String str2 = sc.next();
System.out.println(isAnagram(str1, str2));
I have the following questions with regards to the above code:
1) How can I further improve my approach?
2) Is there a better way to solve this question?
3) Are there any grave code violations that I have committed?
4) Can space and time complexity be further improved?
Reference
java beginner strings interview-questions complexity
add a comment |Â
up vote
2
down vote
favorite
Given two strings, check whether two given strings are anagram of each
other or not. An anagram of a string is another string that contains
same characters, only the order of characters can be different. For
example, âÂÂactâ and âÂÂtacâ are anagram of each other.
Input:
The first line of input contains an integer T denoting the number of
test cases. Each test case consist of two strings in 'lowercase' only,
in a separate line.
Output:
Print "YES" without quotes if the two strings are anagram else print
"NO".
Constraints:
1 ⤠T ⤠30
1 ⤠|s| ⤠100
Example:
Input:
2
geeksforgeeks
forgeeksgeeks
allergy
allergic
Output:
YES
NO
My approach:
/*package whatever //do not write package name here */
import java.util.Scanner;
import java.io.IOException;
import java.util.HashMap;
class GFG
private static String isAnagram (String str1, String str2)
HashMap <Character, Integer>occurs = new HashMap<>();
if (str1.length() != str2.length())
return "NO";
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
for (char ch: str2.toCharArray())
if (!(occurs.containsKey(ch)))
return "NO";
else
int count = occurs.get(ch);
count = count - 1;
if (count < 0)
return "NO";
else
occurs.put(ch,count);
return "YES";
public static void main (String args) throws IOException
Scanner sc = new Scanner (System.in);
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++)
String str1 = sc.next();
String str2 = sc.next();
System.out.println(isAnagram(str1, str2));
I have the following questions with regards to the above code:
1) How can I further improve my approach?
2) Is there a better way to solve this question?
3) Are there any grave code violations that I have committed?
4) Can space and time complexity be further improved?
Reference
java beginner strings interview-questions complexity
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Given two strings, check whether two given strings are anagram of each
other or not. An anagram of a string is another string that contains
same characters, only the order of characters can be different. For
example, âÂÂactâ and âÂÂtacâ are anagram of each other.
Input:
The first line of input contains an integer T denoting the number of
test cases. Each test case consist of two strings in 'lowercase' only,
in a separate line.
Output:
Print "YES" without quotes if the two strings are anagram else print
"NO".
Constraints:
1 ⤠T ⤠30
1 ⤠|s| ⤠100
Example:
Input:
2
geeksforgeeks
forgeeksgeeks
allergy
allergic
Output:
YES
NO
My approach:
/*package whatever //do not write package name here */
import java.util.Scanner;
import java.io.IOException;
import java.util.HashMap;
class GFG
private static String isAnagram (String str1, String str2)
HashMap <Character, Integer>occurs = new HashMap<>();
if (str1.length() != str2.length())
return "NO";
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
for (char ch: str2.toCharArray())
if (!(occurs.containsKey(ch)))
return "NO";
else
int count = occurs.get(ch);
count = count - 1;
if (count < 0)
return "NO";
else
occurs.put(ch,count);
return "YES";
public static void main (String args) throws IOException
Scanner sc = new Scanner (System.in);
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++)
String str1 = sc.next();
String str2 = sc.next();
System.out.println(isAnagram(str1, str2));
I have the following questions with regards to the above code:
1) How can I further improve my approach?
2) Is there a better way to solve this question?
3) Are there any grave code violations that I have committed?
4) Can space and time complexity be further improved?
Reference
java beginner strings interview-questions complexity
Given two strings, check whether two given strings are anagram of each
other or not. An anagram of a string is another string that contains
same characters, only the order of characters can be different. For
example, âÂÂactâ and âÂÂtacâ are anagram of each other.
Input:
The first line of input contains an integer T denoting the number of
test cases. Each test case consist of two strings in 'lowercase' only,
in a separate line.
Output:
Print "YES" without quotes if the two strings are anagram else print
"NO".
Constraints:
1 ⤠T ⤠30
1 ⤠|s| ⤠100
Example:
Input:
2
geeksforgeeks
forgeeksgeeks
allergy
allergic
Output:
YES
NO
My approach:
/*package whatever //do not write package name here */
import java.util.Scanner;
import java.io.IOException;
import java.util.HashMap;
class GFG
private static String isAnagram (String str1, String str2)
HashMap <Character, Integer>occurs = new HashMap<>();
if (str1.length() != str2.length())
return "NO";
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
for (char ch: str2.toCharArray())
if (!(occurs.containsKey(ch)))
return "NO";
else
int count = occurs.get(ch);
count = count - 1;
if (count < 0)
return "NO";
else
occurs.put(ch,count);
return "YES";
public static void main (String args) throws IOException
Scanner sc = new Scanner (System.in);
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++)
String str1 = sc.next();
String str2 = sc.next();
System.out.println(isAnagram(str1, str2));
I have the following questions with regards to the above code:
1) How can I further improve my approach?
2) Is there a better way to solve this question?
3) Are there any grave code violations that I have committed?
4) Can space and time complexity be further improved?
Reference
java beginner strings interview-questions complexity
edited Jul 13 at 18:35
rolflâ¦
90.2k13186390
90.2k13186390
asked Jul 13 at 17:52
Anirudh Thatipelli
42229
42229
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
Your use of the counting system using a HashMap is a functional approach to solving the problem, and algorithmically it's relatively good (has time complexity of $O(N)$). But, for a prolem like this, it's easier to normalize each input value in to a sorted array or string, than in to a counting HashMap
.
Consider the following function:
public static final String normalize(value)
if (value == null)
return null;
char chars = value.toCharArray();
Arrays.sort(chars);
return new String(chars);
Now, all values that are anagrams will have the same normalize
result (they have the same characters in the same sorted order).
Your code would simply become:
private static String isAnagram (String str1, String str2)
return Objects.equals(normalize(str1), normalize(str2));
Note that in this case, Objects
is java.util.Objects
.
Further, by putting the normalization in to a separate function it reduces the amount of logic duplication in your code.
Now, you ask if there are any grave violations... and I have to answer "yes". In Java it is common practice to put {
braces on the same line as the code block definition. For example your code:
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
should be:
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
4
Anirudh simply refuses to use curly braces in a manner consistent with common practice. There are a couple of other things I've seen pointed out to him that he just Doesn't Want To Do. I've given up on being helpful because of it. For fans of streaming, you can also one-line the method asreturn Arrays.equals(s1.codePoints().sorted().toArray(), s2.codePoints().sorted().toArray()) ? "YES" : "NO";
â Eric Stein
Jul 13 at 18:47
@EricStein Sincere apologies. I didn't know that keeping braces on the new line was a huge violation of coding practices. I will try to improve upon it from next time.
â Anirudh Thatipelli
Jul 14 at 3:22
@rofl Isn't sorting the elements take O(n logn) extra time complexity?
â Anirudh Thatipelli
Jul 14 at 3:23
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Your use of the counting system using a HashMap is a functional approach to solving the problem, and algorithmically it's relatively good (has time complexity of $O(N)$). But, for a prolem like this, it's easier to normalize each input value in to a sorted array or string, than in to a counting HashMap
.
Consider the following function:
public static final String normalize(value)
if (value == null)
return null;
char chars = value.toCharArray();
Arrays.sort(chars);
return new String(chars);
Now, all values that are anagrams will have the same normalize
result (they have the same characters in the same sorted order).
Your code would simply become:
private static String isAnagram (String str1, String str2)
return Objects.equals(normalize(str1), normalize(str2));
Note that in this case, Objects
is java.util.Objects
.
Further, by putting the normalization in to a separate function it reduces the amount of logic duplication in your code.
Now, you ask if there are any grave violations... and I have to answer "yes". In Java it is common practice to put {
braces on the same line as the code block definition. For example your code:
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
should be:
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
4
Anirudh simply refuses to use curly braces in a manner consistent with common practice. There are a couple of other things I've seen pointed out to him that he just Doesn't Want To Do. I've given up on being helpful because of it. For fans of streaming, you can also one-line the method asreturn Arrays.equals(s1.codePoints().sorted().toArray(), s2.codePoints().sorted().toArray()) ? "YES" : "NO";
â Eric Stein
Jul 13 at 18:47
@EricStein Sincere apologies. I didn't know that keeping braces on the new line was a huge violation of coding practices. I will try to improve upon it from next time.
â Anirudh Thatipelli
Jul 14 at 3:22
@rofl Isn't sorting the elements take O(n logn) extra time complexity?
â Anirudh Thatipelli
Jul 14 at 3:23
add a comment |Â
up vote
2
down vote
Your use of the counting system using a HashMap is a functional approach to solving the problem, and algorithmically it's relatively good (has time complexity of $O(N)$). But, for a prolem like this, it's easier to normalize each input value in to a sorted array or string, than in to a counting HashMap
.
Consider the following function:
public static final String normalize(value)
if (value == null)
return null;
char chars = value.toCharArray();
Arrays.sort(chars);
return new String(chars);
Now, all values that are anagrams will have the same normalize
result (they have the same characters in the same sorted order).
Your code would simply become:
private static String isAnagram (String str1, String str2)
return Objects.equals(normalize(str1), normalize(str2));
Note that in this case, Objects
is java.util.Objects
.
Further, by putting the normalization in to a separate function it reduces the amount of logic duplication in your code.
Now, you ask if there are any grave violations... and I have to answer "yes". In Java it is common practice to put {
braces on the same line as the code block definition. For example your code:
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
should be:
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
4
Anirudh simply refuses to use curly braces in a manner consistent with common practice. There are a couple of other things I've seen pointed out to him that he just Doesn't Want To Do. I've given up on being helpful because of it. For fans of streaming, you can also one-line the method asreturn Arrays.equals(s1.codePoints().sorted().toArray(), s2.codePoints().sorted().toArray()) ? "YES" : "NO";
â Eric Stein
Jul 13 at 18:47
@EricStein Sincere apologies. I didn't know that keeping braces on the new line was a huge violation of coding practices. I will try to improve upon it from next time.
â Anirudh Thatipelli
Jul 14 at 3:22
@rofl Isn't sorting the elements take O(n logn) extra time complexity?
â Anirudh Thatipelli
Jul 14 at 3:23
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Your use of the counting system using a HashMap is a functional approach to solving the problem, and algorithmically it's relatively good (has time complexity of $O(N)$). But, for a prolem like this, it's easier to normalize each input value in to a sorted array or string, than in to a counting HashMap
.
Consider the following function:
public static final String normalize(value)
if (value == null)
return null;
char chars = value.toCharArray();
Arrays.sort(chars);
return new String(chars);
Now, all values that are anagrams will have the same normalize
result (they have the same characters in the same sorted order).
Your code would simply become:
private static String isAnagram (String str1, String str2)
return Objects.equals(normalize(str1), normalize(str2));
Note that in this case, Objects
is java.util.Objects
.
Further, by putting the normalization in to a separate function it reduces the amount of logic duplication in your code.
Now, you ask if there are any grave violations... and I have to answer "yes". In Java it is common practice to put {
braces on the same line as the code block definition. For example your code:
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
should be:
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
Your use of the counting system using a HashMap is a functional approach to solving the problem, and algorithmically it's relatively good (has time complexity of $O(N)$). But, for a prolem like this, it's easier to normalize each input value in to a sorted array or string, than in to a counting HashMap
.
Consider the following function:
public static final String normalize(value)
if (value == null)
return null;
char chars = value.toCharArray();
Arrays.sort(chars);
return new String(chars);
Now, all values that are anagrams will have the same normalize
result (they have the same characters in the same sorted order).
Your code would simply become:
private static String isAnagram (String str1, String str2)
return Objects.equals(normalize(str1), normalize(str2));
Note that in this case, Objects
is java.util.Objects
.
Further, by putting the normalization in to a separate function it reduces the amount of logic duplication in your code.
Now, you ask if there are any grave violations... and I have to answer "yes". In Java it is common practice to put {
braces on the same line as the code block definition. For example your code:
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
should be:
for (char ch: str1.toCharArray())
if (!(occurs.containsKey(ch)))
occurs.put(ch,1);
else
int count = occurs.get(ch);
occurs.put(ch,count + 1);
answered Jul 13 at 18:31
rolflâ¦
90.2k13186390
90.2k13186390
4
Anirudh simply refuses to use curly braces in a manner consistent with common practice. There are a couple of other things I've seen pointed out to him that he just Doesn't Want To Do. I've given up on being helpful because of it. For fans of streaming, you can also one-line the method asreturn Arrays.equals(s1.codePoints().sorted().toArray(), s2.codePoints().sorted().toArray()) ? "YES" : "NO";
â Eric Stein
Jul 13 at 18:47
@EricStein Sincere apologies. I didn't know that keeping braces on the new line was a huge violation of coding practices. I will try to improve upon it from next time.
â Anirudh Thatipelli
Jul 14 at 3:22
@rofl Isn't sorting the elements take O(n logn) extra time complexity?
â Anirudh Thatipelli
Jul 14 at 3:23
add a comment |Â
4
Anirudh simply refuses to use curly braces in a manner consistent with common practice. There are a couple of other things I've seen pointed out to him that he just Doesn't Want To Do. I've given up on being helpful because of it. For fans of streaming, you can also one-line the method asreturn Arrays.equals(s1.codePoints().sorted().toArray(), s2.codePoints().sorted().toArray()) ? "YES" : "NO";
â Eric Stein
Jul 13 at 18:47
@EricStein Sincere apologies. I didn't know that keeping braces on the new line was a huge violation of coding practices. I will try to improve upon it from next time.
â Anirudh Thatipelli
Jul 14 at 3:22
@rofl Isn't sorting the elements take O(n logn) extra time complexity?
â Anirudh Thatipelli
Jul 14 at 3:23
4
4
Anirudh simply refuses to use curly braces in a manner consistent with common practice. There are a couple of other things I've seen pointed out to him that he just Doesn't Want To Do. I've given up on being helpful because of it. For fans of streaming, you can also one-line the method as
return Arrays.equals(s1.codePoints().sorted().toArray(), s2.codePoints().sorted().toArray()) ? "YES" : "NO";
â Eric Stein
Jul 13 at 18:47
Anirudh simply refuses to use curly braces in a manner consistent with common practice. There are a couple of other things I've seen pointed out to him that he just Doesn't Want To Do. I've given up on being helpful because of it. For fans of streaming, you can also one-line the method as
return Arrays.equals(s1.codePoints().sorted().toArray(), s2.codePoints().sorted().toArray()) ? "YES" : "NO";
â Eric Stein
Jul 13 at 18:47
@EricStein Sincere apologies. I didn't know that keeping braces on the new line was a huge violation of coding practices. I will try to improve upon it from next time.
â Anirudh Thatipelli
Jul 14 at 3:22
@EricStein Sincere apologies. I didn't know that keeping braces on the new line was a huge violation of coding practices. I will try to improve upon it from next time.
â Anirudh Thatipelli
Jul 14 at 3:22
@rofl Isn't sorting the elements take O(n logn) extra time complexity?
â Anirudh Thatipelli
Jul 14 at 3:23
@rofl Isn't sorting the elements take O(n logn) extra time complexity?
â Anirudh Thatipelli
Jul 14 at 3:23
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f198444%2fanagram-checking-implementation%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password