Skip to content

Commit

Permalink
Create ValidAnagram.java
Browse files Browse the repository at this point in the history
  • Loading branch information
harshithasudhakar authored Dec 5, 2024
1 parent 914ed8e commit 9ffecc3
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Strings/ValidAnagram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.Scanner;

class ValidAnagram {
public static boolean isAnagram(String s, String t) {
int count = 0;
Map<Character, Integer> map = new HashMap<>();

if (s.length() != t.length()) {
return false;
}

for(int i = 0; i< s.length(); i++){
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
}

for (int i = 0; i < t.length(); i++) {
char charT = t.charAt(i);
if (map.containsKey(charT) && map.get(charT) > 0) {
map.put(charT, map.get(charT) - 1);
} else {
return false; // If the character doesn't match or has excess frequency
}
}

// If all frequencies are zero, then it's an anagram
for (int value : map.values()) {
if (value != 0) {
return false;
}
}

return true;
}

public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter String 1: ");
String s = input.nextLine();
System.out.println("Enter String 2: ");
String t = input.nextLine();
isAnagram(s, t);
}
}

0 comments on commit 9ffecc3

Please sign in to comment.