Skip to content
On this page

242. Valid Anagram share

Problem Statement

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?

Solution:

go
package main

import "sort"

func isAnagram_eff(s string, t string) bool {
	if len(s) != len(t) {
		return false
	}

	var sMap = make(map[rune]int)
	var tMap = make(map[rune]int)

	for _, v := range s {
		sMap[v]++
	}

	for _, v := range t {
		tMap[v]++
	}

	for k, v := range sMap {
		if tMap[k] != v {
			return false
		}
	}

	return true
}

func isAnagram(s string, t string) bool {
	if len(s) != len(t) {
		return false
	}

	sRunes := []rune(s)
	tRunes := []rune(t)

	sort.Slice(sRunes, func(i, j int) bool {
		return sRunes[i] < sRunes[j]
	})
	sort.Slice(tRunes, func(i, j int) bool {
		return tRunes[i] < tRunes[j]
	})

	for i := 0; i < len(sRunes); i++ {
		if sRunes[i] != tRunes[i] {
			return false
		}
	}

	return true
}

rs
use std::collections::HashMap;

impl Solution {
    // using hashmap
    pub fn is_anagram(s: String, t: String) -> bool {
        if s.len() != t.len() {
            return false;
        }

        let s_map = s.chars().fold(HashMap::new(), |mut map, c| {
            // let count = map.entry(c).or_insert(0);
            // *count += 1;

            // same as above
            *map.entry(c).or_insert(0) += 1;
            map
        });

        let t_map = t.chars().fold(HashMap::new(), |mut map, c| {
            *map.entry(c).or_insert(0) += 1;
            map
        });

        s_map
            .iter()
            .all(|(key, value)| t_map.get(key) == Some(value))
    }

    pub fn is_anagram(s: String, t: String) -> bool {
        if (s.len() != t.len()) {
            return false;
        }

        // create vector of chars from the string
        let mut s_chars = s.chars().collect::<Vec<char>>();
        let mut t_chars = t.chars().collect::<Vec<char>>();

        // sort the vector of chars
        s_chars.sort();
        t_chars.sort();

        // compare the sorted vectors of chars
        for i in 0..s_chars.len() {
            if s_chars[i] != t_chars[i] {
                return false;
            }
        }

        true
    }
}

...


Released under the MIT License.