<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Taosif Jamal</title>
    <description>The latest articles on DEV Community by Taosif Jamal (@taosif7).</description>
    <link>https://dev.to/taosif7</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F162046%2F7ee21e8d-a8f8-41ae-bc42-40a68429567e.jpg</url>
      <title>DEV Community: Taosif Jamal</title>
      <link>https://dev.to/taosif7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/taosif7"/>
    <language>en</language>
    <item>
      <title>Optimise your String Algorithms in Java</title>
      <dc:creator>Taosif Jamal</dc:creator>
      <pubDate>Sat, 10 Sep 2022 18:39:53 +0000</pubDate>
      <link>https://dev.to/taosif7/optimise-your-string-algorithms-in-java-17ef</link>
      <guid>https://dev.to/taosif7/optimise-your-string-algorithms-in-java-17ef</guid>
      <description>&lt;p&gt;While practicing DSA String problems, one thing I stumbled upon many times was trying to find alternatives to make my algorithm faster at some scales. Thus I explored different approaches, and during String questions I had to choose between one way or other so I used to check time &amp;amp; space complexities of operations on string. It took me few google searches to find a satisfactory explaination, but for each operation I gotta google.&lt;/p&gt;

&lt;p&gt;Hence to relieve you from this pain, and to save your time: I am listing Time &amp;amp; Space complexities of various operations on string with straightforward explainations. Note that this is all with respect to Java SE 7 or v1.7&lt;/p&gt;

&lt;h2&gt;
  
  
  Instantiation — new String(“a”)
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(n)
&lt;/h4&gt;

&lt;p&gt;It creates a char array and fills each character of string in array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concatenation — “a” + ”b”
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(mn)
&lt;/h4&gt;

&lt;p&gt;Every time you concat a string a new buffer is created and the contents are copied over.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Strings are immutable in Java.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example: Lets say you concatenate "abc + "def"
Under the hood, Java is performing these operations:
• Construct copy of "abc" with additional length; 
• Copy "d" to new array
• Construct copy of "abcd"
• Copy "e" to new array
• Construct copy of "abcde"
• Copy "f" to new array
So O(m) called n times, thus time complexity is O(mn)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Space Complexity: O(mn)
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Better Approach: StringBuilder
&lt;/h4&gt;

&lt;p&gt;Use StringBuilder with .append() method, It creates internal buffer that expands only on demand. Its time complexity is almost O(m+n).&lt;br&gt;
String.concat is another approach, but slower than StringBuilder.&lt;/p&gt;

&lt;h2&gt;
  
  
  .length()
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(1)
&lt;/h4&gt;

&lt;p&gt;Because string holds a counter variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  .equals()
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(n)
&lt;/h4&gt;

&lt;p&gt;In most cases, its actually faster than O(n), because it checks for some things before hand like length of string is not equal then instantly return false. Also it linearly compares upto first non equal character only, but in the worst case its O(n)&lt;/p&gt;

&lt;h4&gt;
  
  
  Space Complexity: O(1)
&lt;/h4&gt;

&lt;h2&gt;
  
  
  .charAt()
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(1)
&lt;/h4&gt;

&lt;p&gt;It is because in Java, Strings are implemented using char array, and random access is in array’s nature. Hence character at index can be accessed randomly in O(1).&lt;/p&gt;

&lt;h2&gt;
  
  
  .substring()
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(n)
&lt;/h4&gt;

&lt;p&gt;As mentioned before, strings are immutable in Java. Hence for creating a substring of length n, a new string instance needs to be created. Under the hood, java is actually creating a char array of length n and copying elements from original string to new one.&lt;/p&gt;

&lt;h4&gt;
  
  
  Space Complexity: O(n)
&lt;/h4&gt;

&lt;p&gt;It’ll need space to store contents of array.&lt;/p&gt;

&lt;h2&gt;
  
  
  .toCharArray()
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(n)
&lt;/h4&gt;

&lt;p&gt;It should be O(1) since string is nothing but a char array, right? Yes, but no. toCharArray method copies the contents of string to a new char array, hence O(n).&lt;/p&gt;

&lt;h4&gt;
  
  
  Space Complexity: O(n)
&lt;/h4&gt;

&lt;p&gt;Obviously, it’ll need space to store contents of array.&lt;/p&gt;

&lt;h2&gt;
  
  
  .contains() and .indexOf()
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(mn)
&lt;/h4&gt;

&lt;p&gt;.contains() calls .indexOf() method and returns true if index &amp;gt; -1.&lt;br&gt;
It is quite surprising that java uses a naive method (Loop inside loop) for finding a string in another. There are several algorithms out there that do the same job in O(n) like KMP, but for them there’s overhead space and time cost as well. So engineers at Sun/Oracle must have empirically tested various algorithms and decided that naive method works best on average for all kind of scenarios.&lt;/p&gt;

&lt;h4&gt;
  
  
  Space Complexity: O(1)
&lt;/h4&gt;

&lt;h2&gt;
  
  
  .replace(char, char)
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(n)
&lt;/h4&gt;

&lt;p&gt;It basically goes through each character in string, and replaces it with new character. Although with some optimisations&lt;/p&gt;

&lt;h4&gt;
  
  
  Space Complexity: O(n)
&lt;/h4&gt;

&lt;p&gt;Since strings are immutable, new string needs to be created&lt;/p&gt;

&lt;h2&gt;
  
  
  .replace(regex, replacement) and .replaceAll()
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Time Complexity: Oh(its complicated)
&lt;/h4&gt;

&lt;p&gt;Whether your regex string is simple string like “red” or its a complex regular expression pattern, this function under the hood uses Patterns class to match the character and then replace it. Lets try to understand the nature of time complexity here.&lt;br&gt;
Suppose your regex string is he(ll|ter|r)o&lt;br&gt;
It can match hello, hetero, hero from your string.&lt;/p&gt;

&lt;p&gt;The Regex Engine will go through the string, and if it encounters he it’ll intstantly match it, immediately after that it’ll try to match ll if found, it’ll next find o and if o is not found, it’ll go back and try to match ter and you get the point.&lt;/p&gt;

&lt;p&gt;Well, Time complexity of this function is not in our control, but we can optimise our Regex to perform well at matching. From previous example, we can do (hello | hetero | hero) So it reduces back tracking by regex engine to optimise search function and ultimately replace function. Here’s a good resource if you want to learn more about optimising regex patterns.&lt;br&gt;
Bonus: I found a tool to Visualise Regex&lt;/p&gt;

&lt;h4&gt;
  
  
  Space Complexity: O(n)
&lt;/h4&gt;

&lt;p&gt;Remember we can’t mutate a string in Java?&lt;/p&gt;

&lt;h2&gt;
  
  
  .split(separator)
&lt;/h2&gt;

&lt;p&gt;If Separator is single character and not in “.$|()[{^?*+\”:&lt;/p&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(n)
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Space Complexity: O(n)
&lt;/h4&gt;

&lt;p&gt;If Separator string is more than one character, it is compiled into a Regex Pattern by Java for finding index to split, and as discussed in .replace() section, It’s time complexity depends on nature of pattern&lt;/p&gt;

&lt;h4&gt;
  
  
  Time Complexity: Oh(its complicated)
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Space Complexity: O(n)— Because y’kno immutable
&lt;/h4&gt;

&lt;h2&gt;
  
  
  .toLowerCase() and .toUpperCase()
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Time Complexity: O(n)
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Space Complexity: O(n)
&lt;/h4&gt;

&lt;p&gt;Straightforward, each character of string is checked one by one, and new string is stored separately&lt;/p&gt;




&lt;p&gt;That’s all Folks. I hope this article will be helpful to you to optimise your string functions. Did I miss any essential function? let me know if any.&lt;br&gt;
Thanks.&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>java</category>
      <category>string</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
