From a53ad2f6550a5d2a4e94e85da35cb2aa79f3c13e Mon Sep 17 00:00:00 2001 From: Praveenchouhan3462 <79068774+Praveenchouhan3462@users.noreply.github.com> Date: Mon, 31 Oct 2022 09:50:34 +0530 Subject: [PATCH] Create stringsegmentation.py --- stringsegmentation.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 stringsegmentation.py diff --git a/stringsegmentation.py b/stringsegmentation.py new file mode 100644 index 00000000..6d1daeda --- /dev/null +++ b/stringsegmentation.py @@ -0,0 +1,15 @@ +def can_segment_string(s, dictionary): + for i in range(1, len(s) + 1): + first = s[0:i] + if first in dictionary: + second = s[i:] + if not second or second in dictionary or can_segment_string(second, dictionary): + return True + return False + +s = "hellonow"; +dictionary= set(["hello","hell","on","now"]) +if can_segment_string(s, dictionary): + print("String Can be Segmented") +else: + print("String Can NOT be Segmented")