Daily Challenge #39

Longest Common Ending

Write a function that returns the longest common ending between two strings.

Examples

longestCommonEnding("multiplication", "ration") ➞ "ation"

longestCommonEnding("potent", "tent") ➞ "tent"

longestCommonEnding("skyscraper", "carnivore") ➞ ""

Notes

Return an empty string if there exists no common ending.

3 Likes


this is working but as you can see it will return array type result can it be ok??

edit:


sorry i forget about if no similar end so here it is
it’s about 29 blocks

1 Like

well, it is not working ;-)… try the words multiplication and noitac
Taifun

hmm you are right it is not working there…
i quit this i don’t think i can trying for 45 minutes hard to do

1 Like

should i delete my post??

No, you gave it a try and came far. Good job.

1 Like

5 Likes


I have used linear and reverse loop to check & match the characters of Word A with Word B. In liner loop if the sreing is not empty it’ll display the result, else if the sreing is empty then it’ll run a reverse loop and check the charecters again… and then display the result.

with this method i tried these and i got the result as cation

Also if i’m wrong then please let me know… :relaxed:

2 Likes

1 Like

@Hossein the solution to the last example shoulld be t rather than *empty string*
Taifun

This seems to work

5 Likes

the solution for potent and tnet should be t rather than empty string… :wink:
Taifun

2 Likes

Not according to @Peter requirement. I think should be empty string. That’s why I was also returning empty string

1 Like

You were right, I hope this one does the job

1 Like

potent and pot, both words end with t… as well as potent and tnet… so isn’t that the longest common ending then?
Taifun

1 Like

Aha, then I think I misunderstood the full requirement