-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from mohshin-shah/31-minimum-window-with-all-c…
…haracters-from-other-string feat: Add `PermutationOfPatternInString` algorithm fixes #31
- Loading branch information
Showing
3 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// PermutationOfPatternInString.swift | ||
// | ||
// | ||
// Created by Mohshinsha Shahmadar on 2024-09-09. | ||
// | ||
|
||
import Foundation | ||
/** | ||
Problem Statement | ||
Given a string and a pattern, find out if the string contains any permutation of the pattern. | ||
|
||
Example 1: | ||
|
||
Input: str="oidbcaf", pattern="abc" | ||
Output: true | ||
Explanation: The string contains "bca" which is a permutation of the given pattern. | ||
Example 2: | ||
|
||
Input: str="odicf", pattern="dc" | ||
Output: false | ||
Explanation: No permutation of the pattern is present in the given string as a substring. | ||
Example 3: | ||
|
||
Input: str="bcdxabcdy", pattern="bcdyabcdx" | ||
Output: true | ||
Explanation: Both the string and the pattern are a permutation of each other. | ||
Example 4: | ||
|
||
Input: str="aaacb", pattern="abc" | ||
Output: true | ||
Explanation: The string contains "acb" which is a permutation of the given pattern. | ||
*/ | ||
func findPermutationOfPatternInString( | ||
string: String, | ||
pattern p: String | ||
) -> Bool { | ||
var windowStart = 0 | ||
let array = Array(string) | ||
let pattern = Array(p) | ||
|
||
let patternMap: [Character: Int] = pattern.reduce(into: [:]) { map, char in | ||
map[char, default: 0] += 1 | ||
} | ||
|
||
var matchCount = 0 | ||
var windowMap = [Character: Int]() | ||
|
||
for (windowEnd, char) in string.enumerated() { | ||
windowMap.increment(char) | ||
|
||
// Check if the frequency of the current character matches that in the patternMap | ||
if let patternFreq = patternMap[char], patternFreq == windowMap[char] { | ||
matchCount += 1 | ||
} | ||
|
||
if windowEnd >= pattern.count { | ||
let windowStartChar = array[windowStart] | ||
|
||
if let pC = patternMap[windowStartChar], | ||
let wC = windowMap[windowStartChar], | ||
pC == wC { | ||
matchCount -= 1 | ||
} | ||
|
||
windowMap.decrement(windowStartChar) | ||
windowStart += 1 | ||
} | ||
|
||
if matchCount == patternMap.count { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
29 changes: 29 additions & 0 deletions
29
Tests/SwiftyAlgorithmsTests/SlidingWindow/MinimumWindowMatchingPatternTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// PermutationOfPatternInStringTests.swift | ||
// | ||
// | ||
// Created by Mohshinsha Shahmadar on 2024-09-09. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import SwiftyAlgorithms | ||
|
||
final class PermutationOfPatternInStringTests: XCTestCase { | ||
func testPermutationOfPatternInString() { | ||
|
||
let testCases: [AlgorithmTestCase] = [ | ||
.init((string: "oidbcaf", pattern: "abc"), true), | ||
.init((string: "odicf", pattern: "dc"), false), | ||
.init((string: "bcdxabcdy", pattern: "bcdyabcdx"), true), | ||
.init((string: "aaacb", pattern: "abc"), true), | ||
] | ||
|
||
testAlgorithm( | ||
name: "PermutationOfPatternInStringTests", | ||
with: testCases | ||
) { input in | ||
findPermutationOfPatternInString(string: input.string, pattern: input.pattern) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters