🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Anagrams are two words or phrases that rearrange the letters of one another to form the other. For instance, the word "listen" is an anagram of "silent". In this post, we'll detail the steps to determine whether two strings are anagrams using TypeScript.
2. Program Overview
Our TypeScript program will standardize the two strings by removing any non-alphanumeric characters and making them lowercase. After that, the program will compare the sorted characters of the two strings to determine if they are anagrams.
3. Code Program
// Function to determine if two strings are anagrams
function areAnagrams(str1: string, str2: string): boolean {
// Standardize the strings
const sanitizedStr1 = str1.replace(/\W/g, '').toLowerCase();
const sanitizedStr2 = str2.replace(/\W/g, '').toLowerCase();
// Sort and compare
return sanitizedStr1.split('').sort().join('') === sanitizedStr2.split('').sort().join('');
}
// Test the areAnagrams function
const string1 = "Listen";
const string2 = "Silent";
console.log(`"${string1}" and "${string2}" are anagrams: ${areAnagrams(string1, string2)}`);
Output:
"Listen" and "Silent" are anagrams: true
4. Step By Step Explanation
1. We begin by defining the areAnagrams function, which takes two strings as parameters.
2. Inside the function, we first standardize both input strings. This involves:
- Removing any non-alphanumeric characters using the regular expression /\W/g.
- Converting the strings to lowercase using the toLowerCase() method. This ensures the comparison is case-insensitive.
3. Once the strings are standardized, we:
- Split each string into an array of characters with the split('') method.
- Sort the arrays with the sort() method.- Join the sorted arrays back into strings with the join('') method.
4. We then compare the sorted strings using the equality operator (===). If they match, the two original strings are anagrams.
5. Finally, we test our areAnagrams function with two example strings and display the results using console.log.
By following this approach, the TypeScript program can efficiently determine if two strings are anagrams.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment