Given two strings s
and t
, return true
if t
is an anagram of s
, and false
otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = "anagram", t = "nagaram" Output: true
Example 2:
Input: s = "rat", t = "car" Output: false
Constraints:
1 <= s.length, t.length <= 5 * 104
s
and t
consist of lowercase English letters.Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
program main
use anagram_solution
implicit none
character(len=:), allocatable :: s, t
logical :: result
! Example 1
s = 'anagram'
t = 'nagaram'
result = is_anagram(s, t)
write (unit=*, fmt='(L1)') result
! Example 2
s = 'rat'
t = 'car'
result = is_anagram(s, t)
write (unit=*, fmt='(L1)') result
end program main
temp.f95:2:9: 2 | use anagram_solution | 1 Fatal Error: Cannot open module file βanagram_solution.modβ for reading at (1): No such file or directory compilation terminated.
! This script solves the problem of determining whether one string is an anagram of another.
! The program runs with all provided examples and outputs to stdout.
program anagram
implicit none
! Declare variables
character(len=50) :: s, t
logical :: is_anagram
! Read input from stdin
read(*,*) s
read(*,*) t
! Check if t is an anagram of s
is_anagram = .false.
if (len(s) == len(t)) then
is_anagram = .true.
do i = 1, len(s)
if (index(t, s(i:i)) == 0) then
is_anagram = .false.
exit
end if
end do
end if
! Output result to stdout
if (is_anagram) then
write(*,*) "true"
else
write(*,*) "false"
end if
end program anagram
temp.f95:20:8: 20 | do i = 1, len(s) | 1 Error: Symbol βiβ at (1) has no IMPLICIT type
def is_anagram(s, t):
if len(s) != len(t):
return False
counts = {}
for c in s:
if c in counts:
counts[c] += 1
else:
counts[c] = 1
for c in t:
if c not in counts or counts[c] == 0:
return False
counts[c] -= 1
return True
The algorithm first checks if the two input strings have the same length. If they do not, it returns false
, because an anagram of a string would have the same length as the original string.
Then, the algorithm initializes a hash table to store the frequency of each character in the string s
. It iterates through the string s
, and for each character, it increments the corresponding frequency count in the hash table.
It then iterates through the string t
. For each character in t
, it checks if the frequency count of that character in the hash table is greater than 0. If it is not, that means the character either wasn't in the string s
, or has been used up already, so the function returns false
. Otherwise, it decrements the frequency count for the current character in the hash table.
Finally, if the function has not yet returned false
, that means that the two strings are anagrams and the function returns true
.
bool isAnagram(std::string s, std::string t) {
if(s.size() != t.size())
return false;
std::unordered_map<char, int> counts;
for(char c : s)
counts[c]++;
for(char c : t) {
if(counts[c] == 0)
return false;
counts[c]--;
}
return true;
}
The algorithm first checks if the two input strings have the same length. If they do not, it returns false
, because an anagram of a string would have the same length as the original string.
Then, the algorithm initializes a hash table to store the frequency of each character in the string s
. It iterates through the string s
, and for each character, it increments the corresponding frequency count in the hash table.
It then iterates through the string t
. For each character in t
, it checks if the frequency count of that character in the hash table is greater than 0. If it is not, that means the character either wasn't in the string s
, or has been used up already, so the function returns false
. Otherwise, it decrements the frequency count for the current character in the hash table.
Finally, if the function has not yet returned false
, that means that the two strings are anagrams and the function returns true
.