Menu Sidebar
Menu

strings

[LintCode] Longest Palindromic Substring

public String longestPalindrome(String s) { // Write your code here if(s.length() == 0 || s == null) return “”; int start = 0; int end = 0; for(int i = 0; i < s.length(); i++) { int len1 = expand(s, i, i); int len2 = expand(s, i, i+1); int len = Math.max(len1, len2); if(len > […]

[LintCode] Rotate String

public void rotateString(char[] str, int offset) { // write your code here if(offset == 0 || str.length == 0) return; offset = offset % str.length; reverse(str, 0, str.length – 1 – offset); reverse(str, str.length – offset, str.length – 1); reverse(str, 0 , str.length – 1); } public void reverse(char[] c, int i, int j) { […]

[LintCode] Longest Words

ArrayList<String> longestWords(String[] dictionary) { // write your code here ArrayList<String> res = new ArrayList<String>(); if(dictionary == null || dictionary.length == 0) return res; int len = dictionary[0].length(); for(int i = 0; i < dictionary.length; i++) { if(dictionary[i].length() == len) res.add(dictionary[i]); else if(dictionary[i].length() > len){ len = dictionary[i].length(); res.clear(); res.add(dictionary[i]); } } return res; }

[LintCode] String to Integer(atoi)

public int atoi(String str) { // write your code here if(str == null || str.length() == 0) return 0; str = str.trim(); int i = 0; char sign = ‘+’; if(str.charAt(0) == ‘+’) i++; else if(str.charAt(0) == ‘-‘){ i++; sign = ‘-‘; } int res = 0; while(i < str.length() && str.charAt(i) >=’0′ && str.charAt(i)<=’9’) […]

[LintCode] Longest Common Prefix

public String longestCommonPrefix(String[] strs) { // write your code here if(strs == null || strs.length == 0) return “”; String prefix = strs[0]; for(int i = 1; i < strs.length; i++) { int j = 0; while(j < prefix.length() && j < strs[i].length() && prefix.charAt(j)==strs[i].charAt(j)) j++; prefix = prefix.substring(0,j); } return prefix; }

[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(); […]

Codeforces Round #307 (Div. 2) B. ZgukistringZ

原题: http://codeforces.com/contest/551/problem/B 题目大意: 给3个string,a,b,c. 问用a的字母通过交换,最多可以得到多少个b或者c? 分析: 本来感觉是dp的题, 后来想想不用那么复杂, 先用三个26个字符数组, 存下三个字符串的字频, 然后用三个变量,num, nums_a, nums_b存下对应a,b,c的个数. 然后暴力破解就可以, 因为题目是求最多b或者c. 我们假设从0个b开始,一步步递增. 看看最多能有多少个. 这里需要注意的是, b的取值应该是从[0,count_b[j]*i > count_a[j]](0个b, 到i个b的第j个字母超过了i个a的第j个字母). 最后输出的时候, 先输出b,再是c, 最后把a剩下的字符输出就行了 public void solve(int testNumber, InputReader in, OutputWriter out) { String a = in.readString(); String b = in.readString(); String c = in.readString(); int[] ca = new int[26]; int nums = 0; int […]

Codeforces Round #309 (Div. 2) B. Ohana Cleans Up

原题:http://codeforces.com/contest/554/problem/B 题目大意: 给个n x n的binary矩阵, 只能翻转(0->1,1->0)列, 问如何翻转, 让全是1行的总数最大. 分析: 因为每次翻转列的时候, 每个行都要翻转, 所以只要找到最大数目的相同的行. 就可以了. public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); HashMap<String, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { String s = in.readString(); if (!map.containsKey(s)) map.put(s,0); map.put(s,map.get(s)+1); } int max = 0 […]

Codeforces Round #313 (Div. 2) D. Equivalent Strings

原题:http://codeforces.com/contest/560/problem/D 题目大意: 给字符串a和b, 定义a和b是等价的,如果: a和b相等. a等分成a1,a2,b等分成b1,b2, (a1==b1 && a2==b2) || (a2==b1 && a1==b2) 分析: 就暴呗…我也不知道为什么我暴就报错, 是因为java的原因么, 我看人家同样码用c++, 都飞快的. public void solve(int testNumber, InputReader in, OutputWriter out) { String a = in.readString(); String b = in.readString(); if (solve(a,b)) out.print(“YES”); else out.print(“NO”); } private boolean solve(String a, String b) { if (a.length() % 2 != 0 || […]

Newer Posts

书脊

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

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930