code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

242 Valid Anagram

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
* Given two strings s and t , write a function to determine if t is an anagram of s.
* 
* Example 1:
* 
* Input: s = "anagram", t = "nagaram"
* Output: true
* Example 2:
* 
* Input: s = "rat", t = "car"
* Output: false
* Note:
* You may assume the string contains only lowercase alphabets.
*/

function validAnagram(s, t) {
  if(s.length !== t.length) {
    return false
  }
  let charMap = {}
  s = s.split('')
  for (let index = 0; index < s.length; index++) {
    if(charMap[s[index]]) {
      charMap[s[index]] += 1
    } else {
      charMap[s[index]] = 1  
    }
  }
  for (let index = 0; index < t.length; index++) {
    if(!charMap[t[index]]){
      return false
    } else {
      charMap[t[index]]--
    }
  }
  return true
}

test('valid Anagram', () => {
  expect(validAnagram('anagram', 'nagaram')).toEqual(true)
  expect(validAnagram('rat', 'car')).toEqual(false)
});

Created 2020-03-14T21:52:31+00:00 · Edit