Answer to Question #176678 in Python for G V S Bhasker Nikhil Patchipulusu

Question #176678

Given a sentence as input, find all the unique combinations of two words and print the word combinations that are not adjacent in the original sentence in lexicographical order.


1
Expert's answer
2021-03-29T12:32:12-0400
def generate(st, s):
    if len(s) == 0:
        return
  
    # If current string is not already present.
    if s not in st:
        st.add(s)
  
        # Traverse current string, one by one
        # remove every character and recur.
        for i in range(len(s)):
            t = list(s).copy()
            t.remove(s[i])
            t = ''.join(t)
            generate(st, t)
  
    return
  
  
# Driver Code
if __name__ == "__main__":
    s = "xyz"
    st = set()
    generate(st, s)
    for i in st:
        print(i)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS