Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,683 questions

55,435 answers

573 users

How to count the number of non-overlapping instances of a substring in a string in Swift

1 Answer

0 votes
import Foundation

// Non‑overlapping occurrences are matches of a substring that do not reuse any of 
// the same characters. Once one match is counted, the next search must begin 
// after that match ends.

func countNonOverlapping(haystack: String, needle: String) -> Int {
    /*
    Count how many times 'needle' appears in 'haystack' without overlapping.
    The algorithm:
      • Use str.find() to locate the next occurrence.
      • Each time a match is found, move the search index forward
        by the full length of the matched substring.
      • This ensures no characters are reused between matches.
    */
    // Handle edge case for empty needle to prevent infinite loop
    if needle.isEmpty { return 0 }
    
    var count = 0
    var index = haystack.startIndex  // current search position in the main string
 
    // Continue searching until .find() returns -1 (meaning: no more matches)
    while true {
        // Find the next occurrence starting at the current index
        if let range = haystack.range(of: needle, options: [], range: index..<haystack.endIndex) {
            // We found a match, so increment the count
            count += 1
            
            // Move index forward by the length of the needle
            // This ensures the next search begins *after* the matched substring
            index = range.upperBound
        } else {
            // No more matches found
            break
        }
    }
 
    return count
}
 
// ---------------------------------------------------------------
// Demonstration using the string provided in the instructions:
let s = "go java phphp rust c pphpp c++ phpphp python php phphp"
let substring = "php"
 
// Count non-overlapping occurrences
let result = countNonOverlapping(haystack: s, needle: substring)
 
print("Non-overlapping occurrences: \(result)")



/*
run:
 
Non-overlapping occurrences: 6
 
*/

 



answered Jul 18 by avibootz

Related questions

...