Tensorum
YouTubeसीखना शुरू करें

String

Two pointers, palindromes, character counting और KMP व rolling hash से string matching।

DSAशुरुआती16 घंटे8 पैटर्न · 29 प्रॉब्लम
0%
आपकी प्रगति

प्रॉब्लम और केस स्टडी के नाम, कोड और तकनीकी कीवर्ड अंग्रेज़ी में ही रखे गए हैं।

मूल विचार

तय alphabet से गिनती O(1) space में होती है (26-int arrays)। Palindromes केंद्र से फैलते हैं; matching दोबारा scan से बचने के लिए prefix-function (KMP) या rolling hash का उपयोग करती है।

कब इस्तेमाल करें

  • Anagram, palindrome या substring वाले प्रश्न
  • बड़े टेक्स्ट के अंदर pattern matching
  • Characters की frequency जवाब तय करती है

टेम्पलेट

def longest_palindrome(s):
    best = ''
    for c in range(len(s)):
        for l, r in ((c, c), (c, c + 1)):
            while l >= 0 and r < len(s) and s[l] == s[r]:
                l, r = l - 1, r + 1
            if r - l - 1 > len(best):
                best = s[l + 1:r]
    return best

Complexity

Counting O(n); expand-around-center O(n²); KMP / Rabin-Karp O(n + m)।

आम गलतियाँ

  • Loop में string जोड़ना O(n²) है — इकट्ठा करें और join करें
  • सम-लंबाई वाले palindromes पर off-by-one
  • Rabin-Karp में बिना जाँच के hash collisions

पहचान के संकेत

HashingTwo PointersPalindromeKMP/String MatchingSliding Window