From 5788f3aa0f89079e28e490f57fd74fd5e738580e Mon Sep 17 00:00:00 2001 From: Youknow55 <93278661+Youknow55@users.noreply.github.com> Date: Sun, 30 Oct 2022 21:26:49 +0530 Subject: [PATCH] Create Longest Common Prefix --- 2022-Oct/18 Oct 2022/Longest Common Prefix | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 2022-Oct/18 Oct 2022/Longest Common Prefix diff --git a/2022-Oct/18 Oct 2022/Longest Common Prefix b/2022-Oct/18 Oct 2022/Longest Common Prefix new file mode 100644 index 0000000..92741ba --- /dev/null +++ b/2022-Oct/18 Oct 2022/Longest Common Prefix @@ -0,0 +1,18 @@ +class Solution { +public: + string longestCommonPrefix(vector& a) { + + + string ans = ""; + for (int i = 0; i < a[0].size(); i++) { + char cur = a[0][i]; + for (string s : a) { + if (s.size() == i or s[i] != cur) return ans; + } + ans += cur; + } + return ans; + + + } +};