Determine whether two strings are anagrams.

Determine whether two strings are anagrams.

Create a helper function that accepts a string as input. This function begins by creating an empty object of type?{[char:string]: number}?to hold character counts. Using a?for...of?loop, the function iterates over each character in the string, implicitly converting it to lowercase using iterables. This eliminates the need to explicitly call?toLowerCase()for each character. A regular expression checks if the current character is a lowercase letter. If it is, bracket notation accesses the character's count in the object. The logical OR assignment operator initializes the count to 0 if the character doesn't yet exist, then increments it by 1. This approach effectively handles both existing and new characters. The function ultimately returns the object, which now maps characters to their frequencies in the input string.

The main function accepts two strings as input. It calls the helper function twice to obtain the character count objects for both strings. The function then checks if the lengths of the key sets (unique characters) are equal in both strings. If they aren't, the strings can't be anagrams because they have different character sets. In this case, the function immediately returns?false.

If the lengths of the key sets are equal, indicating both strings have the same number of unique characters, the function proceeds to a?for...in?loop. This loop iterates through each character key, checking if the count of a character in the first string matches the count of the same character in the second string. If the counts differ, the function returns?false?to indicate that the strings aren't anagrams.

If the function runs through the loop without finding any discrepancies in the character counts, it returns?true, signifying that the strings are anagrams.

function isAnagram(str1: string, str2:string): boolean{
  const getCharCountObj = (str: string) => {
    const charCountObj: {[char:string]:number}  = {};
    for(let char of str.toLowerCase()){
      if(char.match(/[a-z]/)){
        charCountObj[char] = (charCountObj[char] || 0) + 1;
      }
    }
    return charCountObj;
  }
  const charCount1 = getCharCountObj(str1);
  const charCount2 = getCharCountObj(str2);
  if(Object.keys(charCount1).length !== Object.keys(charCount2).length){
    return false
  }
  for(let char in charCount1){
    if(charCount1[char] !== charCount2[char]){
      return false;
    } 
  }

  return true
}

console.log(isAnagram("silent", "enlist"))        

My recent publication compiled a comprehensive collection of 100 similar typescript programs. Each program not only elucidates essential Typescript concepts and expounds upon the significance of test automation but also provides practical guidance on its implementation using Playwright. This resource will certainly be valuable if you look deeper into similar topics.


要查看或添加评论,请登录

Cerosh Jacob的更多文章

  • Playwright API Automation: A Hands-On Guide

    Playwright API Automation: A Hands-On Guide

    This project demonstrates Playwright's effectiveness for API testing and highlights its user-friendly nature. The code…

    2 条评论
  • Playwright vs RestAssured: Should API Testing Be Combined?

    Playwright vs RestAssured: Should API Testing Be Combined?

    Pros of Using Playwright for API Testing: Playwright's framework provides several advantages for API testing…

  • ??Test Automation Framework Evolution?

    ??Test Automation Framework Evolution?

    This is a project to showcase the evolution of the Playwright test automation framework and its integration into…

  • Find Common Elements Between Two Arrays.

    Find Common Elements Between Two Arrays.

    : This symbol indicates that the function is generic. It can work with any type, not just specific ones like numbers or…

  • Identify the missing number in an array of consecutive numbers.

    Identify the missing number in an array of consecutive numbers.

    This function accepts an array of numbers as input and returns a number representing the missing number, or if the…

  • Merge two sorted arrays

    Merge two sorted arrays

    The function accepts two sorted arrays of numbers as arguments and returns a single, merged array of numbers. An empty…

  • Find the Maximum Number of Words.

    Find the Maximum Number of Words.

    The function takes an array of strings and returns a number. A variable is declared and set to 0 to store the maximum…

  • Find the sum of elements in a given array.

    Find the sum of elements in a given array.

    The function is defined using the keyword, assigning an arrow function to the constant. This function also takes an…

  • Remove a substring from a string using a string

    Remove a substring from a string using a string

    By default, the method in TypeScript only replaces the first occurrence of the pattern in the string if the pattern is…

  • Remove vowels using regular expressions.

    Remove vowels using regular expressions.

    The function accepts a string as input and returns a new string with all vowels removed. This is accomplished by using…

社区洞察

其他会员也浏览了