I am trying to implement a function in JavaScript called canBeFormed
that does this:
const string1 = 'hellooooolooo'
const string2 = 'hellolo'
canBeFormed(string1, string2) // true since by deleting the repeated chars 'o' and 'l' we can form the word 'hellolo'
So if string2
can be formed by string1
if string1
remove some duplicate chars then we return true
I cannot seem to come up with a workable solution to this problem. Can someone give this a shot?
Btw, someone mentioned that this could be solved by dfs + backtracking. Can someone give that approach a try?
To clarify, this function return true
if it can form a word from the second string provided by removing one or more repeated letters.
So canBeFormed("heellolo", "hellooooolooo")
will return false
. canBeFormed("abc", "ac")
will return false
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…