How to check whether "silent" and "listen" are equal

Hi all,

I have a problem we have two string inputs and we want to search that whether these two are equal in terms of rearranging the letters, for e.g- “silent” and “listen” reaaranging them will form the other word. So the output should contain they are equal, but when i write " hi" and “how are you” they shouldn’t be equal , will the logic involve index of the letter?
@Palaniyappan @postwick @supermanPunch

Hi @Jai_Pande
Use first six syntax in Assign activity and condition in If.

inputString1= "silent"

inputString2= "listen"

inputString1 = inputString1.Replace(" ", "").ToLower()

inputString2 = inputString2.Replace(" ", "").ToLower()

sortedString1 = New String(inputString1.ToCharArray().OrderBy(Function(c) c).ToArray())

sortedString2 = New String(inputString2.ToCharArray().OrderBy(Function(c) c).ToArray())

If sortedString1 = sortedString2
Then
    // The strings are equal when rearranging the letters.
    // Add your desired actions here.
    LogMessage(“The strings are equal.”)
Else
    // The strings are not equal when rearranging the letters.
    // Add your desired actions here.
    LogMessage(“The strings are not equal.”)
End If

Hope it helps!!

Can i use sort also?

Hi @Jai_Pande

inputString1= "silent"

inputString2= "listen"

inputString1 = inputString1.Replace(" ", "").ToLower()

inputString2 = inputString2.Replace(" ", "").ToLower()

sortedString1 = New String(inputString1.ToCharArray().Sort().ToArray())

sortedString2 = New String(inputString2.ToCharArray().Sort().ToArray())

If sortedString1 = sortedString2
Then
    // The strings are equal when rearranging the letters.
    // Add your desired actions here.
    LogMessage(“The strings are equal.”)
Else
    // The strings are not equal when rearranging the letters.
    // Add your desired actions here.
    LogMessage(“The strings are not equal.”)
End If

Hope it helps!!

I need to handle the spaces as well okay for the we have replace

One more thing i get it why its converting first to char array so that it can sort individual letters but again why its converting back to array? Please let me know

After conversion it will be in the form of string only. Check the syntax of sortedString1 and sortedString2

Regards

@Jai_Pande

Simply you can try this

Str1="Silent"
Str2="Listen"
Anagram=String.Join("", str1.ToLower().OrderBy(Function(c) c)).Equals(String.Join("", str2.ToLower().OrderBy(Function(c) c)))

BlankProcess14.zip (61.7 KB)

Hope this helps!!

Thanks alot man! Okay my question was that why in the previous code its written To array in the end of the linq query why we are using to array in the end

@Jai_Pande

Based on logic we have to implement the code there are lot of ways. In the previous method it is storing into array and then compare the two strings.

Hope it’s clarified @Jai_Pande

If yes would recommend to close this topic.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.