Some Useful JAVA String technique
in Algorithms
1. When Concatenating String…
- Do use Character array or String Builder
- Do not use str +=str, The time complexity for this method is O(n^2) !
Character array : O(n)
String a ="BoilingPoint"; char[] chars = a.toCharArray();
StringBuilder : O(n)
StringBuilder str = new StringBuilder(); for(int i=0;i<n;i++){ str.append("hello"); } String s = str.toString();
2. When Storing Substring of s that contains characters…
Map can be a great solution
According to ASCII table, 65~90 : Capital Letters, 97~122 : Lowercase Letters.
This method shows better performance,
- Leetcode20) Valid Parentheses
- Leetcode 161) One Edit Distance
- Leetcode187) Repeated DNA Sequences
- Leetcode242) Valid Anagram
- Leetcode394) Decode String - string builder