校招算法笔面试 | 华为机试-删除字符串中出现次数最少的字符

题目

题目链接

解题思路

  1. 使用哈希表(字典)统计每个字符出现的次数
  2. 找出最小出现次数
  3. 遍历原字符串,只保留出现次数大于最小次数的字符

代码

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

int main() {
    string str;
    cin >> str;
    // 统计字符频率
    unordered_map<char, int> freq;
    for (char c : str) {
        freq[c]++;
    }
     
    // 找出最小频率
    int minFreq = str.length();
    for (auto& pair : freq) {
        minFreq = min(minFreq, pair.second);
    }
    
    // 构建结果字符串
    string result;
    for (char c : str) {
        if (freq[c] > minFreq) {
            result += c;
        }
    }
     
    cout << result << endl;
    return 0;
}
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.nextLine();
            // 统计字符频率
            Map<Character, Integer> freq = new HashMap<>();
            for (char c : str.toCharArray()) {
                freq.put(c, freq.getOrDefault(c, 0) + 1);
            }
            
            // 找出最小频率
            int minFreq = str.length();
            for (int count : freq.values()) {
                minFreq = Math.min(minFreq, count);
            }
            
            // 构建结果字符串
            StringBuilder result = new StringBuilder();
            for (char c : str.toCharArray()) {
                if (freq.get(c) > minFreq) {
                    result.append(c);
                }
            }
            
            System.out.println(result.toString());
        }
    }
}
s = input()
# 统计字符频率
freq = {}
for c in s:
   freq[c] = freq.get(c, 0) + 1
   
# 找出最小频率
min_freq = min(freq.values())

# 构建结果字符串
result = ''.join(c for c in s if freq[c] > min_freq)

print(result)

算法及复杂度

  • 算法:哈希表统计 + 字符串处理
  • 时间复杂度: O ( n ) \mathcal{O}(n) O(n),其中 n 为字符串长度
  • 空间复杂度: O ( k ) \mathcal{O}(k) O(k),其中 k 为不同字符的个数,最大为26(因为题目限定只有小写字母)

题目链接

解题思路

  1. 题目要求:

    • 计算两个字符串的最长公共子串长度
    • 子串必须连续
    • 只包含小写字母
    • 字符串长度: 1 ≤ s ≤ 150 1 \leq s \leq 150 1s150
  2. 解题方法:动态规划

    • 使用二维dp数组记录公共子串长度
    • d p [ i ] [ j ] dp[i][j] dp[i][j] 表示以 s t r 1 [ i ] str1[i] str1[i] s t r 2 [ j ] str2[j] str2[j] 结尾的最长公共子串长度
    • 如果当前字符相同,则 d p [ i ] [ j ] = d p [ i − 1 ] [ j − 1 ] + 1 dp[i][j] = dp[i-1][j-1] + 1 dp[i][j]=dp[i1][j1]+1

代码

def longest_common_substring(str1, str2):
    m, n = len(str1), len(str2)
    dp = [[0] * (n + 1) for _ in range(m + 1)]
    max_len = 0
    
    # 填充dp数组
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if str1[i-1] == str2[j-1]:
                dp[i][j] = dp[i-1][j-1] + 1
                max_len = max(max_len, dp[i][j])
    
    return max_len

while True:
    try:
        str1 = input().strip()
        str2 = input().strip()
        print(longest_common_substring(str1, str2))
    except:
        break
import java.util.*;

public class Main {
    public static int longestCommonSubstring(String str1, String str2) {
        int m = str1.length(), n = str2.length();
        int[][] dp = new int[m + 1][n + 1];
        int maxLen = 0;
        
        // 填充dp数组
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (str1.charAt(i-1) == str2.charAt(j-1)) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                    maxLen = Math.max(maxLen, dp[i][j]);
                }
            }
        }
        
        return maxLen;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str1 = sc.nextLine();
            String str2 = sc.nextLine();
            System.out.println(longestCommonSubstring(str1, str2));
        }
    }
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int longestCommonSubstring(string str1, string str2) {
    int m = str1.length(), n = str2.length();
    vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
    int maxLen = 0;
    
    // 填充dp数组
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (str1[i-1] == str2[j-1]) {
                dp[i][j] = dp[i-1][j-1] + 1;
                maxLen = max(maxLen, dp[i][j]);
            }
        }
    }
    
    return maxLen;
}

int main() {
    string str1, str2;
    while (getline(cin, str1) && getline(cin, str2)) {
        cout << longestCommonSubstring(str1, str2) << endl;
    }
    return 0;
}

算法分析

  • 算法:动态规划
  • 时间复杂度: O ( m × n ) \mathcal{O}(m \times n) O(m×n),其中 m m m n n n 是两个字符串的长度
  • 空间复杂度: O ( m × n ) \mathcal{O}(m \times n) O(m×n),需要二维dp数组
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值