Implementing an efficent algorithm to find the intersection of two strings

前端 未结 5 1921
挽巷
挽巷 2020-12-18 13:29

Implement an algorithm that takes two strings as input, and returns the intersection of the two, with each letter represented at most once.

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-18 13:36

    "with each letter represented at most once"

    I'm assuming that this means you just need to know the intersections, and not how many times they occurred. If that's so then you can trim down your algorithm by making use of yield. Instead of storing the count and continuing to iterate the second string looking for additional matches, you can yield the intersection right there and continue to the next possible match from the first string.

提交回复
热议问题