Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public String findLongestWord(String s, List<String> d) {
- int len = 0, clen = 0;
- String res = "";
- for(String w : d)
- {
- if(lcs(s, w))
- {
- len = w.length();
- if(len >= clen)
- {
- if(len > clen) res = w;
- if(len == clen && res.compareTo(w) >= 0)
- res = w;
- clen = len;
- }
- }
- }
- return res;
- }
- boolean lcs(String a, String b)
- {
- int m = a.length(), n = b.length(), i = 0, j = 0;
- if(m < n) return false;
- while(i < m && j < n)
- {
- if(a.charAt(i) == b.charAt(j))
- {
- i++;
- j++;
- }
- else i++;
- }
- return j == n;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment