Menu Sidebar
Menu

LintCode

[LintCode] Longest Common Substring

public int longestCommonSubstring(String A, String B) { // write your code here int max = 0; int[][] dp = new int[A.length()+1][B.length()+1]; for(int i = 1 ; i <= A.length(); i++) { for(int j = 1 ; j <= B.length(); j++) { if(A.charAt(i-1) == B.charAt(j-1)) dp[i][j] = dp[i-1][j-1] + 1; else dp[i][j] = 0; max = […]

[LintCode] Anagrams

public List<String> anagrams(String[] strs) { // write your code here List<String> res = new ArrayList<String>(); if(strs.length == 0 || strs == null) return res; HashMap<Integer,ArrayList<String>> maps = new HashMap<Integer,ArrayList<String>>(); for(int i = 0 ; i < strs.length; i++) { String s = strs[i]; int[] count = new int[26]; for(int j = 0; j < s.length(); […]

[LintCode] strStr

/** * Returns a index to the first occurrence of target in source, * or -1 if target is not part of source. * @param source string to be scanned. * @param target string containing the sequence of characters to match. */ public int strStr(String source, String target) { //write your code here if(target == […]

[LintCode] Compare Strings

public boolean compareStrings(String A, String B) { // write your code here int[] count_A = new int[26]; for(int i = 0; i < A.length(); i++) count_A[A.charAt(i)-‘A’]++; int[] count_B = new int[26]; for(int i = 0; i < B.length(); i++) count_B[B.charAt(i)-‘A’]++; for(int i = 0 ; i < 26; i++) { if(count_A[i]<count_B[i]) return false; } return […]

[LintCode] Two Strings Are Anagrams

public boolean anagram(String s, String t) { // write your code here s = s.toLowerCase(); t = t.toLowerCase(); int[] count_s = new int[26]; for(int i = 0; i < s.length(); i++){ if(s.charAt(i) == ‘ ‘) continue; count_s[s.charAt(i)-‘a’]++; } int[] count_t = new int[26]; for(int i = 0; i < t.length(); i++){ if(t.charAt(i) == ‘ ‘) […]

Newer Posts

书脊

这青苔碧瓦堆, 俺曾睡风流觉, 将五十年兴亡看饱.

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930