LintCode 78: Longest Common Prefix
- Longest Common Prefix
中文English
Given k strings, find the longest common prefix (LCP).
Example
Example 1:
Input: “ABCD”, “ABEF”, “ACEF”
Output: “A”
Example 2:
Input: “ABCDEFG”, “ABCEFG” and “ABCEFA”
Output: “ABC”
解法1:hash map
代码如下:
class Solution {
public:/*** @param strs: A list of strings* @return: The longest common prefix*/string longestCommonPrefix(vector<string> &strs) {int N = strs.size();if (N == 0) return "";unordered_map<string, vector<string>> hashmap;for (int i = 0; i < N; ++i) {int M = strs[i].size();for (int j = 0; j < M; ++j) {string pre = strs[i].substr(0, j + 1);hashmap[pre].push_back(strs[i]);}}string result;int maxLen = 0;for (auto h : hashmap) {if (h.second.size() == N) {if (h.first.size() > maxLen) {maxLen = h.first.size();result = h.first;}}}return result;}
};
解法2: Trie
TBD
LintCode 78: Longest Common Prefix相关推荐
- LeetCode:14. Longest Common Prefix
两年硕士超快的鸭,又要准备秋招啦!0508第一题~ 题目 Write a function to find the longest common prefix string amongst an ar ...
- LeetCode - Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 以第一个字 ...
- 【Leetcode】14、Longest Common Prefix
题目 Write a function to find the longest common prefix string amongst an array of strings. If there i ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. Subscribe to ...
- LeetCode上求最长公共字符前缀字符串问题——Longest Common Prefix
1.题目如下: Write a function to find the longest common prefix string amongst an array of strings. If th ...
- Leet Code OJ 14. Longest Common Prefix [Difficulty: Easy]
题目: Write a function to find the longest common prefix string amongst an array of strings. 翻译: 写一个函数 ...
- 【LeetCode算法题库】Day5:Roman to Integer Longest Common Prefix 3Sum
[Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...
- LeetCode14 Longest Common Prefix
题意: Write a function to find the longest common prefix string amongst an array of strings. (Easy) 这两 ...
最新文章
- sws_scale sws_getContext
- ExtJs 3 自定义combotree
- 牛客 - 牛牛的Link Power II(线段树)
- oracle sql 表个数,【oracleSQL】查看当前用户各个表的记录数
- app图标圆角角度_教你如何绘制风格统一的APP界面图标
- P4945-最后的战役【dp,离散化】
- The Shapes of CSS
- 在新项目中要思考的技术点
- 聊聊restful和restframework
- 使用async 和 await方法来
- tf.nn.bidirectional_dynamic_rnn()函数详解
- vs2019键盘钩子_江门网站建设:针对Web开发人员的12个最佳Visual Studio代码扩展
- 希沃集控系统流媒体服务器未开启,希沃集控,让教育信息化管理尽在“掌控”之中...
- 你需要启用steam社区界面功能以进行购买_steam官网手机版免费下载-steam官网手机客户端下载...
- 计算机广告制作教程,Photoshop实例教程:制作网站横条广告
- 【Zeekr_Tech】初谈我们共同的目标 NPDS + Agile
- CentOS部署ScyllaDB 3.3集群
- 关于在win10电脑开启移动热点,手机连上wifi显示无互联网连接的问题
- [CISCN2019 华东南赛区]Web4
- ssh登陆之忽略known_hosts文件