2023-12-16 Daily Challenge

Today I have done leetcode's December LeetCoding Challenge with cpp.

December LeetCoding Challenge 16

Description

Valid Anagram

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

class Solution {
public:
  bool isAnagram(string s, string t) {
    vector<int> sc(26);
    vector<int> tc(26);
    for(auto c : s) sc[c - 'a'] += 1;
    for(auto c : t) tc[c - 'a'] += 1;
    return sc == tc;
  }
};

// Accepted
// 42/42 cases passed (7 ms)
// Your runtime beats 76.44 % of cpp submissions
// Your memory usage beats 20.94 % of cpp submissions (7.9 MB)