Welcome to the String Processing template collection! These are ready-to-use Java snippets for the core string patterns: sliding window, two pointers, string matching, manipulation, and parsing. If you already know the array templates, you’re most of the way there — strings use the same ideas with character-level twists. See also Arrays & Strings for KMP and rolling hash.
String problems are array problems in disguise. Most string patterns — sliding window, two pointers, prefix computation — work identically to their array counterparts. The main difference is that you operate on characters and often track frequencies with a hash map or fixed-size array.
Summary
| Pattern | Signal Phrases | Key Idea |
|—|—|—|
| Sliding Window | “longest substring”, “minimum window” | Track char frequencies in window |
| Two Pointers | “palindrome”, “reverse” | Compare from both ends |
| String Matching | “pattern in text”, “KMP” | Failure function for O(n+m) |
| Manipulation | “anagram”, “group anagrams” | Sort or frequency count |
| Parsing | “decode string”, “nested brackets” | Stack-based recursion |
When to use: The problem asks for “longest substring without repeating characters”, “minimum window containing all characters”, or any contiguous substring optimization with a frequency constraint.
When to use: The problem asks to “find a pattern in text”, mentions “KMP”, or requires efficient O(n+m) substring search instead of brute-force O(n·m).
KMP Algorithm
ID
Title
Link
Solution
28
Find the Index of the First Occurrence in a String
int[]buildKMP(Stringpattern){intm=pattern.size();int[]lps=newint[m];intlen=0,i=1;while(i<m){if(pattern[i]==pattern[len]){lps[i++]=++len;}else{if(len!){len=lps[len-1];}else{lps[i++]=0;}}}returnlps;}staticintkmpSearch(Stringtext,Stringpattern){intn=text.size(),m=pattern.size();int[]lps=buildKMP(pattern);inti=0,j=0;while(i<n){if(text.charAt(i)==pattern[j]){i++;j++;}if(j==m){returni-j;// Found at index i - j}elseif(i<n&&text.charAt(i)!=pattern[j]){if(j!){j=lps[j-1];}else{i++;}}}return-1;}
ID
Title
Link
Solution
28
Find the Index of the First Occurrence in a String
When to use: The problem says “anagram”, “group anagrams”, “remove duplicates”, or requires rearranging or classifying strings by their character composition.
// import java.util.*;// Remove All Adjacent DuplicatesstaticStringremoveDuplicates(Strings){Stringresult;for(charc:s.toCharArray()){if(!result.isEmpty()&&result.get(result.size()-1)==c){result.removeLast();}else{result.add(c);}}returnresult;}// Remove All Adjacent Duplicates II (k duplicates)staticStringremoveDuplicates(Strings,intk){List<List<char>>st=newArrayList<>();for(charc:s.toCharArray()){if(!st.isEmpty()&&st.get(st.size()-1).first==c){st.get(st.size()-1).second++;if(st.get(st.size()-1).second==k){st.removeLast();}}else{st.add(newint[]{c,1});}}Stringresult;for(vare:st.entrySet()){result.append(count,c);}returnresult;}
staticbooleanvalidWordAbbreviation(Stringword,Stringabbr){inti=0,j=0;intn=word.size(),m=abbr.size();while(i<n&&j<m){if(isdigit(abbr[j])){if(abbr[j]=='0')returnfalse;// Leading zerointnum=0;while(j<m&&isdigit(abbr[j])){num=num10+(abbr[j]-'0');j++;}i+=num;}else{if(word.charAt(i)!=abbr[j])returnfalse;i++;j++;}}returni==n&&j==m;}