Find All Anagrams in a String - Java Solution (Leetcode - 438)



We would be solving this problem using 

  • Sliding Window approach with 1 Hashmap
  • First we will populate pattern map with occurrence of chars in pattern map.
  • Additionally, a new indicator distinctCharsInMapValueMoreThanZero will be created. It's initial value would be map.size(). This variable is for numbers of chars in map with value > 0. This will ensure we don't have to compare all values of map to see, if all characters are zero or not.
  • When variable distinctCharsInMapValueMoreThanZero is zero, it means current substring is anagram of pattern. 
  • When traversing string via Sliding window, we will keep reduce count of character in pattern map.
  • In map, we will not push any char, which are not part of pattern.

Comments

Popular posts from this blog

Sliding Window Maximum (Maximum of all subarrays of size k) - Java Solution - Leetcode 239

Count occurrence of anagrams in String