其他
面试中的两个字符串问题竟给我整懵了?! | 原力计划
/**
* @author roadway
*/
public class TestString {
public static int judgeTwoString(String a, String b) {
char[] aChar = a.toCharArray();
char[] bChar = b.toCharArray();
int r = 0;
for (int i = 0;i < aChar.length ; i ++){
for(int j = 0; j < bChar.length; j++){
if(aChar[i] == bChar[j]) {
r++;
}
}
}
return r;
}
public static void main(String[] args) {
System.out.println(judgeTwoString("abcdef", "bcd"));
}
}
import java.util.HashMap;
import java.util.Map;
/**
* @author roadway
*/
public class TestString {
public static int judgeTwoString(String a, String b) {
Map<Character, Integer> map = new HashMap<>();
int count = 0;
for(char c : a.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
for(int i = 0; i < b.length(); i++) {
count += map.getOrDefault(b.charAt(i), 0);
}
return count;
}
public static void main(String[] args) {
System.out.println(judgeTwoString("abcdef", "bcd"));
}
}
/**
* @author roadway
*/
public class TestString {
public static int judgeTwoString(String a, String b) {
int count=0;
for (int i=0;i<a.length();i++){
for (int j=0;j<b.length();j++){
if (b.charAt(j) == a.charAt(i)){
count+=1;
}
}
}
return count;
}
public static void main(String[] args) {
System.out.println(judgeTwoString("abcdef", "bcd"));
}
}
/**
* @author roadway
*/
public class TestString {
public static int judgeTwoString(String a, String b) {
return a.replaceAll("[^" + b + "]", "").length();
}
public static void main(String[] args) {
System.out.println(judgeTwoString("abcdef", "bcd"));
}
}
import java.util.HashSet;
import java.util.Set;
/**
* @author roadway
*/
public class TestString {
public static int judgeTwoString(String a, String b) {
Set<Character> set = new HashSet<>();
for (char c : a.toCharArray()) {
set.add(c);
}
int res = 0;
for (char c : b.toCharArray()) {
if (set.contains(c)) {
res++;
}
}
return res;
}
public static void main(String[] args) {
System.out.println(judgeTwoString("abcdef", "bcd"));
}
}
/**
* @author roadway
*/
public class TestString {
public static void judgeString(String str) {
int max_length = 0;
String max_str = "";
while (str.length() > 0) {
int length = str.length();
String first = str.substring(0, 1);
str = str.replaceAll(first, "");
if (max_length < length - str.length()) {
max_length = length - str.length();
max_str = first;
}
}
System.out.println(max_length + max_str);
}
public static void main(String[] args) {
judgeString("abbcccddddaa");
}
}
更多精彩推荐
☞马化腾朋友圈晒微信支付分:835;爱奇艺回应用户隐私话题;Firefox 77.0 发布| 极客头条
☞韩版马化腾:在大财阀围堵下仍白手起家的凤凰男,抢滩加密交易平台、公链赛道
☞Uber 前无人驾驶工程师告诉你,国内无人驾驶之路还要走多久?