当前位置: 首页 > article >正文

LeetCode //C - 567. Permutation in String

567. Permutation in String

Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.

In other words, return true if one of s1’s permutations is the substring of s2.
 

Example 1:

Input: s1 = “ab”, s2 = “eidbaooo”
Output: true
Explanation: s2 contains one permutation of s1 (“ba”).

Example 2:

Input: s1 = “ab”, s2 = “eidboaoo”
Output: false

Constraints:
  • 1 < = s 1. l e n g t h , s 2. l e n g t h < = 1 0 4 1 <= s1.length, s2.length <= 10^4 1<=s1.length,s2.length<=104
  • s1 and s2 consist of lowercase English letters.

From: LeetCode
Link: 567. Permutation in String


Solution:

Ideas:

1. Precompute frequency counts:

  • We create two frequency arrays count1 (for s1) and count2 (for s2’s first window of size len1).

2. Compare initial window:

  • If count1 matches count2, return true.

3. Sliding window mechanism:

  • Slide through s2 by removing the leftmost character and adding the next character in sequence.
  • Update matches to track how many frequency counts match.

4. Efficiency:

  • Time Complexity: O(n) (where n is the length of s2)
  • Space Complexity: O(1) (only 26 fixed-size arrays)
Code:
bool checkInclusion(char* s1, char* s2) {
    int len1 = strlen(s1), len2 = strlen(s2);
    if (len1 > len2) return false;

    int count1[26] = {0}, count2[26] = {0};

    // Initialize frequency counts for s1 and the first window in s2
    for (int i = 0; i < len1; i++) {
        count1[s1[i] - 'a']++;
        count2[s2[i] - 'a']++;
    }

    // Check if the first window is a permutation
    int matches = 0;
    for (int i = 0; i < 26; i++) {
        if (count1[i] == count2[i]) matches++;
    }

    // Sliding window through s2
    for (int i = 0; i < len2 - len1; i++) {
        if (matches == 26) return true;

        // Remove the first character of the window
        int left = s2[i] - 'a';
        count2[left]--;
        if (count2[left] == count1[left]) {
            matches++;
        } else if (count2[left] + 1 == count1[left]) {
            matches--;
        }

        // Add the next character to the window
        int right = s2[i + len1] - 'a';
        count2[right]++;
        if (count2[right] == count1[right]) {
            matches++;
        } else if (count2[right] - 1 == count1[right]) {
            matches--;
        }
    }

    return matches == 26;
}

http://www.kler.cn/a/529583.html

相关文章:

  • 2025年1月22日(网络编程 udp)
  • 从0开始,来看看怎么去linux排查Java程序故障
  • 99.24 金融难点通俗解释:MLF(中期借贷便利)vs LPR(贷款市场报价利率)
  • jvm - GC篇
  • CNN的各种知识点(一):卷积神经网络CNN通道数的理解!
  • pytorch基于 Transformer 预训练模型的方法实现词嵌入(tiansz/bert-base-chinese)
  • IM 即时通讯系统-42-基于netty实现的IM服务端,提供客户端jar包,可集成自己的登录系统
  • 【Redis】Redis 经典面试题解析:深入理解 Redis 的核心概念与应用
  • java基础概念63-多线程
  • 【xdoj-离散线上练习】T251(C++)
  • AI技术路线(marked)
  • LeetCode 344: 反转字符串
  • Zabbix 推送告警 消息模板 美化(钉钉Webhook机器人、邮件)
  • 无人机飞手光伏吊运、电力巡检、农林植保技术详解
  • kamailio的kamctl的使用
  • [c语言日寄]C语言类型转换规则详解
  • ZYNQ-AXI DMA+AXI-S FIFO回环学习
  • DirectShow过滤器开发-读视频文件过滤器(再写)
  • 本地缓存~
  • 功防世界 Web_php_include
  • 理解红黑树
  • word2vec 实战应用介绍
  • Kotlin 协程 与 Java 虚拟线程对比测试(娱乐性质,请勿严谨看待本次测试)
  • VSCode设置内容字体大小
  • DeepSeek R1本地化部署 Ollama + Chatbox 打造最强 AI 工具
  • 【 软件测试项目实战】 以淘宝网购物车管理功能为例