commit 9839f33accf1187e951dcfc0a86f37e52296c04e
parent 805837bb0add796598723c64d8a5fb1362194e0f
Author: Jack Sangdahl <jack@pngu.org>
Date: Thu, 5 Mar 2026 21:44:35 -0700
1784. check if binary string has at most one segment of ones
Diffstat:
2 files changed, 36 insertions(+), 0 deletions(-)
diff --git a/check_binary_str_one_seg.c b/check_binary_str_one_seg.c
@@ -0,0 +1,19 @@
+// 1784. Check if Binary String Has at Most One Segment of Ones
+#include "leetcode.h"
+
+/*
+ * given a binary string 's' without leading zeros, return 'true' if 's'
+ * contains at most one contiguous segment of ones. otherwise, return false.
+ */
+
+bool checkOnesSegment(char *s) { return !strstr(s, "01"); }
+
+int main() {
+ char *s1 = "1001", *s2 = "110";
+ bool r1 = checkOnesSegment(s1);
+ bool r2 = checkOnesSegment(s2);
+ printf("%d\n", r1); // expect: 0
+ assert(r1 == false);
+ printf("%d\n", r2); // expect: 1
+ assert(r2 == true);
+}
diff --git a/check_binary_str_one_seg.py b/check_binary_str_one_seg.py
@@ -0,0 +1,17 @@
+# 1784. Check if Binary String Has at Most One Segment of Ones
+
+"""
+given a binary string 's' without leading zeros, return 'true' if 's'
+contains at most one contiguous segment of ones. otherwise, return false.
+"""
+
+
+class Solution:
+ def checkOnesSegment(self, s: str) -> bool:
+ return "01" not in s
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.checkOnesSegment(s="1001"))
+ print(obj.checkOnesSegment(s="110"))