Header Ads Widget

Responsive Advertisement

02:Javascript DSA | string anagram

String anagram Problem

Today we are going to understand anagram and how to solve this type of question.


string anagram javascript problem


Q:) We have two strings, now we have to check that all characters must be the same in both strings and that the same character's quantity as well must be present.

Sol:
let stringOne="hello"

let stringTwo="ollhe"

now first we have  to make steps or conditions to solve the problem:

condition 1: both string lengths must be equal

 condition 2; character quantity, how many characters come in how many times in both string 

let's solve the problem with these conditions 
we have to make two objects and in which key is the character and the value is how many times this character comes in strings 

function sol(string1, string2) {
    if (string1.length !== string2.length) {
        return `length not eqUAL false `
    }
    let obj = {}
    for (let value of string1) {
        obj[value] = (obj[value] || 0) + 1
    }
    console.log(obj, "string1 obj")
    for (let value2 of string2) {
        // console.log(value2)
        // console.log(obj[value2], obj)
        if (!obj[value2]) {
            return false
        }
        obj[value2] -= 1;
    };
    return true;
};
var result = sol('hello', 'ollhe')
console.log(result)


EXPLANATION OF ABOVE CODE

1: Function Declaration:

The function sol takes two parameters: string1 and string2.

2: Length Check:

The function first checks if the lengths of string1 and string2 are not equal. If they are not equal, it returns the message "length not equal false".

3: Object Initialization:

If the lengths are equal, an empty object obj is created. This object will be used to store the characters of string1 as keys and their respective frequencies as values.

4: Counting Characters in string1:

The function iterates through each character of string1 using a for...of loop.

It updates the obj by incrementing the count of each character encountered. If the character is not present in obj, it initializes its count to 1.

5: Logging obj (Optional):

At this point, the function logs the contents of obj to the console. This step is for debugging purposes and can be commented out.

6: Checking Characters in string2:

Next, the function iterates through each character of string2 using another for...of loop.

For each character, it checks if the character exists in obj.

If the character doesn't exist in obj, it means string2 contains a character that doesn't exist in string1, so the function returns false.

If the character exists in obj, it decrements its count in obj.

7: Final Result:

If the loop completes without returning false, it means all characters in string2 exist in string1 with the same frequencies. Hence, the function returns true.

8: Function Call:

The function is called with parameters 'hello' and 'ollhe', and the result is stored in the variable result.

9:Logging Result:

The result is then logged to the console.

Summary:

 This function determines if string2 is a rearrangement of the characters in string1 by counting the occurrences of each character in both strings and comparing them. If they have different lengths or contain different characters, it returns false, otherwise true.

Feel free to reach out and let us know what your thoughts for this question or any other way to achieve the result. 

Post a Comment

0 Comments