{"id":1806,"date":"2016-09-29T05:50:57","date_gmt":"2016-09-29T05:50:57","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1806"},"modified":"2019-05-20T18:40:23","modified_gmt":"2019-05-20T18:40:23","slug":"string-problems","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/string-problems\/","title":{"rendered":"String Programs"},"content":{"rendered":"<p><strong>String Questions<\/strong><\/p>\n<ol>\n<li>How to find the Longest Substring in String<\/li>\n<li>Swapping Letters in 2 Words<\/li>\n<li>Print String in Reverse Order<\/li>\n<li>Print Left Right Angle Triangle<\/li>\n<li>Inverted Right Triangle<\/li>\n<li>Print Right Angle Triangle<\/li>\n<li>Print Pyramid<\/li>\n<li>Sum of Pairs in Array of Numbers<\/li>\n<li>Check 2nd List has all elements in 1st List<\/li>\n<li>How to find String is anagram<\/li>\n<li>Check Palindrome<\/li>\n<li>Reverse of String without for loop and array<\/li>\n<li>Check Palindrome with recursion<\/li>\n<\/ol>\n<p><strong>1.How to find the Longest Substring in String<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\nString strSample = \"ABCAAABCD\"\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>ABCD\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void main(String&#x5B;] args) \r\n{\r\n   String Test = &quot;ABCAAABCD&quot;;\r\n   String dummy = &quot;&quot;; \r\n\t\t\r\n   String&#x5B;] arrTest = Test.split(&quot;&quot;);\r\n\t\t\r\n   List&lt;String&gt; seenChars = new ArrayList&lt;String&gt;();\r\n   List&lt;String&gt; subStrings = new ArrayList&lt;String&gt;();\r\n\t\t\r\n   int j = 0;\r\n\t\t\r\n   for (int i = 1; i &lt; arrTest.length; i++) \r\n   {\r\n      if(seenChars.contains(arrTest&#x5B;i]))\r\n      {\r\n\t subStrings.add(dummy);\r\n\t dummy = &quot;&quot;;\r\n\t seenChars.clear();\r\n      }\r\n\t\t\t\r\n      seenChars.add(arrTest&#x5B;i]);\r\n      dummy += arrTest&#x5B;i];\r\n\t\t\t\r\n       j = i + 1;\r\n\t\t\t\r\n       if(j == arrTest.length)\r\n\t  subStrings.add(dummy);\r\n   }\r\n\t\t\r\n   int highestNo = 0;\r\n   int lastHighest = 0;\r\n   int currHighestNo = 0;\r\n   int highestPOS = 0;\r\n\t\r\n   for(int k = 0; k &lt; subStrings.size() ; k++)\r\n   {\r\n\tcurrHighestNo = subStrings.get(k).length();\r\n\t\t\r\n\tif(currHighestNo  &gt; lastHighest)\r\n\t{\r\n  \t  highestNo = currHighestNo;\r\n\t  highestPOS = k;\r\n\t}\t\t\r\n\t  lastHighest = currHighestNo;\r\n\t}\r\n\t\t\r\n\tSystem.out.println(subStrings.get(highestPOS));\t\t\r\n}\r\n<\/pre>\n<p><strong>Method 2<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void main(String args&#x5B;]) \r\n{\r\n\tString Test = &quot;ABCDEAAABCDA&quot;;\r\n\tString dummy = &quot;&quot;;\r\n\r\n\tString&#x5B;] arrTest = Test.split(&quot;&quot;);\r\n\tString longestSubString = &quot;&quot;;\r\n\r\n\tList&lt;String&gt; seenChars  = new ArrayList&lt;String&gt;();\r\n\tList&lt;String&gt; subStrings = new ArrayList&lt;String&gt;();\r\n\r\n\tfor (String strTest : arrTest) \r\n\t{\r\n\t\tif (seenChars.contains(strTest)) \r\n\t\t{\r\n\t\t\tif (dummy.length() &gt; longestSubString.length())\r\n\t\t\t\tlongestSubString = dummy;\r\n\r\n\t\t\tsubStrings.add(dummy);\r\n\t\t\tdummy = &quot;&quot;;\r\n\t\t\tseenChars.clear();\r\n\t\t}\r\n\t\t\r\n\t\tseenChars.add(strTest);\r\n\t\tdummy += strTest;\r\n\t}\r\n\t\r\n\tif (dummy.length() &gt; longestSubString.length())\r\n\t{\r\n\t\tlongestSubString = dummy;\t\t\r\n\t\tsubStrings.add(dummy);\t\r\n\t}\r\n\t \r\n\tSystem.out.println(&quot;Longest subString is: &quot; + &quot;'&quot; + longestSubString + &quot;'&quot; + &quot; and length is: &quot;\r\n\t\t\t+ longestSubString.length());\r\n}\r\n<\/pre>\n<p><strong>2.Swapping Letters in 2 Words<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>MUGIL XXX\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>MXUXGXIL\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Array1 \r\n{\r\n static List arrNewList1 = new ArrayList();\r\n \r\n public static void main(String&#x5B;] args) \r\n { \r\n   String text1 = &quot;MUGIL&quot;;\r\n   String text2 = &quot;XXX&quot;; \r\n \r\n   String&#x5B;] arrList1 = text1.split(&quot;&quot;);\r\n   String&#x5B;] arrList2 = text2.split(&quot;&quot;);\r\n \r\n   if(arrList2.length &gt; arrList1.length)\r\n    swapArrays(arrList2, arrList1);\r\n   else \r\n    swapArrays(arrList1, arrList2);\r\n\r\n \r\n \r\n   StringBuilder strBr = new StringBuilder();\r\n \r\n   for (int i = 0; i &lt; arrNewList1.size(); i++) \r\n    strBr.append(arrNewList1.get(i));\r\n \r\n    System.out.println(strBr); \r\n }\r\n \r\n public static void swapArrays(String&#x5B;] pBigList, String&#x5B;] pSmallList)\r\n { \r\n   for (int i = 0; i &lt; pBigList.length; i++) \r\n   { \r\n     arrNewList1.add(pBigList&#x5B;i]);\r\n \r\n     if(i &lt; pSmallList.length)\r\n      arrNewList1.add(pSmallList&#x5B;i]);\r\n   }\r\n }\r\n}\r\n<\/pre>\n<p><strong>Note<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n .\r\n .\r\n String Text1 = &quot;M,U,G,I,L&quot;;\r\n String Text2 = &quot;MUGIL&quot;;\r\n\t\t\r\n System.out.println(Text1.split(&quot;,&quot;).length);\r\n System.out.println(Text2.split(&quot;&quot;).length);\r\n .\r\n .\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n5\r\n6\r\n<\/pre>\n<p>you may be puzzled why first prints 5 and second prints 6.Thats because the empty space between <strong>&#8220;M<\/strong> is taken as 1 element in <strong>Text2<\/strong><\/p>\n<p><strong>3.Print String in Reverse Order<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\nasanam\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nasanam\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.test;\r\n\r\npublic class ReverseString {\r\n\r\n public static void main(String&#x5B;] args) {\r\n  String strName = &quot;manasa&quot;;\r\n\r\n  \/\/char&#x5B;] arrNames = strName.toCharArray();\r\n\r\n  String&#x5B;] arrNames = strName.split(&quot;&quot;);\r\n\r\n  for (int i = arrNames.length - 1; i &gt;= 0; i--) {\r\n   System.out.print(arrNames&#x5B;i]);\r\n  }\r\n }\r\n}\r\n<\/pre>\n<p><strong>4.Print Left Right Angle Triangle<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\nTotal number of Rows - 5\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n* \r\n** \r\n*** \r\n**** \r\n***** \r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.test;\r\n\r\npublic class Patterns2 {\r\n public static void main(String&#x5B;] args) {\r\n  int rows = 5;\r\n\r\n  for (int i = 1; i &lt;= rows; i++) {\r\n   for (int j = 1; j &lt;= i; j++) {\r\n    System.out.print(&quot;*&quot;);\r\n   }\r\n\r\n   System.out.println(&quot; &quot;);\r\n  }\r\n }\r\n}\r\n<\/pre>\n<p><strong>5.Inverted Right Triangle<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\nTotal number of Rows - 5\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n***** \r\n**** \r\n*** \r\n** \r\n*  \r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n.\r\nint rows = 5;\r\n\r\nfor (int i = rows; i &gt;= 0; i--) {\r\n for (int j = 1; j &lt;= i; j++)\r\n  System.out.print(&quot;*&quot;);\r\n\r\n System.out.println(&quot; &quot;);\r\n}\r\n.\r\n<\/pre>\n<p><strong>6.Print Right Angle Triangle<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\nTotal number of Rows - 5\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n     * \r\n    ** \r\n   *** \r\n  **** \r\n *****  \r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nint n = 5;\r\nint j = 0;\r\nint k = 0;\r\n\r\nfor (int i = 0; i &lt; n; i++) {\r\n for (j = n - i; j &gt; 1; j--)\r\n  System.out.print(&quot; &quot;);\r\n\r\n for (j = 0; j &lt;= i; j++)\r\n  System.out.print(&quot;*&quot;);\r\n\r\n System.out.println();\r\n\r\n}\r\n<\/pre>\n<p><strong>7.Print Pyramid<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\nTotal number of Rows - 5\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n    * \r\n   * * \r\n  * * * \r\n * * * * \r\n* * * * *  \r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nint n = 5;\r\nint j = 0;\r\nint k = 0;\r\n\r\nfor (int i = 0; i &lt; n; i++) \r\n{\r\n for (j = n - i; j &gt; 1; j--)\r\n  System.out.print(&quot; &quot;);\r\n\r\n for (j = 0; j &lt;= i; j++)\r\n  System.out.print(&quot;* &quot;);\r\n\r\n System.out.println();\r\n\r\n}\r\n<\/pre>\n<p><em>The only Difference between Right Angle Triangle and Pyramid is Space in * <\/em><\/p>\n<p><strong>8.Sum of Number Pairs in Array<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\nArray of Numbers - {0,1,2,2,3,4}\r\nSum - 4\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n(0,4)\r\n(1,3)\r\n(2,2)\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">.\r\nInteger&#x5B;] arrNumbers = { 0, 1, 2, 2, 3, 4, 5};\r\nint sum = 4;\r\n\r\nfor (int i = 0; i &lt; arrNumbers.length; i++) {\r\n for (int j = i + 1; j &lt; arrNumbers.length; j++) {\r\n  if (arrNumbers&#x5B;i] + arrNumbers&#x5B;j] == sum) {\r\n   System.out.println(&quot;(&quot; + arrNumbers&#x5B;i] + &quot;,&quot; + arrNumbers&#x5B;j] + &quot;)&quot;);\r\n  }\r\n }\r\n}\r\n.\r\n<\/pre>\n<p><strong>9.Check 2nd List has all elements in 1st List<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\n2 List of Elements\r\n{1,2,3,4}{2,3}\r\n{1,2,3,4}{2,5}\r\n{2,3}{2,5,6}  \r\n{2,3}{2,3,9}  \r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n{1,2,3,4}{2,3}  -> true\r\n{1,2,3,4}{2,5}  -> false\r\n{2,3}{2,5,6}    -> false\r\n{2,3}{2,3,9}    -> false\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic boolean checkEquals()\r\n{\r\n  Integer orig&#x5B;] = {1,2,3,4};\r\n  Integer act&#x5B;] = {2,3};\r\n           \r\n  List origList = new ArrayList(Arrays.asList(orig));\r\n  List actList = Arrays.asList(act);\r\n   \r\n  origList.retainAll(actList);\r\n  System.out.println(origList);\r\n      \r\n  if(actList.size()==origList.size())   \r\n    return true;\r\n  else\r\n    return false; \r\n}\r\n<\/pre>\n<p><strong>10.How to find String is anagram?<\/strong><br \/>\n<strong>Output<\/strong><\/p>\n<pre>\r\nKeep and Peek are anagrams\r\nsilent and listen are anagrams\r\nMotherInLaw and HitlerWoman are anagrams\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class AnagramString \r\n{\r\n static void isAnagram(String str1, String str2) \r\n {\r\n  String s1 = str1.replaceAll(&quot;\\\\s&quot;, &quot;&quot;);\r\n  String s2 = str2.replaceAll(&quot;\\\\s&quot;, &quot;&quot;);\r\n  boolean status = true;\r\n\r\n  if (s1.length() != s2.length()) \r\n   status = false;\r\n  else \r\n  {\r\n   char&#x5B;] ArrayS1 = s1.toLowerCase().toCharArray();\r\n   char&#x5B;] ArrayS2 = s2.toLowerCase().toCharArray();\r\n   Arrays.sort(ArrayS1);\r\n   Arrays.sort(ArrayS2);\r\n   status = Arrays.equals(ArrayS1, ArrayS2);\r\n  }\r\n\r\n  if (status)\r\n   System.out.println(s1 + &quot; and &quot; + s2 + &quot; are anagrams&quot;);\r\n  else\r\n   System.out.println(s1 + &quot; and &quot; + s2 + &quot; are not anagrams&quot;);  \r\n }\r\n\r\n public static void main(String&#x5B;] args) \r\n {\r\n  isAnagram(&quot;Keep&quot;, &quot;Peek&quot;);\r\n  isAnagram(&quot;silent&quot;, &quot;listen&quot;);\r\n  isAnagram(&quot;Mother In Law&quot;, &quot;Hitler Woman&quot;);\r\n }\r\n}\r\n<\/pre>\n<p><strong>11.Check Palindrome<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\nmalayalam\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nEntered string is a palindrome\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n public static void main(String args&#x5B;])\r\n {\r\n      String original, reverse = &quot;&quot;;\r\n      original = &quot;madam&quot;;\r\n \r\n      int length = original.length();\r\n \r\n      for ( int i = length - 1; i &gt;= 0; i-- )\r\n         reverse = reverse + original.charAt(i);\r\n \r\n      if (original.equals(reverse))\r\n         System.out.println(&quot;Entered string is a palindrome.&quot;);\r\n      else\r\n         System.out.println(&quot;Entered string is not a palindrome.&quot;); \r\n  }\r\n<\/pre>\n<p><strong>12.Reverse of String without for loop and array<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\nmugil\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nligum\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nstatic String reverseMe(String s) \r\n{\r\n   if(s.length() &lt;= 1)\r\n     return s;\r\n\r\n   return s.charAt(s.length() - 1) + reverseMe(s.substring(0,s.length()-1));\r\n }\r\n<\/pre>\n<p><strong>13.Check palindrome using recursion<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\nmalayalam\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\npalindrome\r\n<\/pre>\n<p><strong>Call the above reverse function and check with string equals method<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static boolean isPalindrome(String input) \r\n{\r\n if (input == null)\r\n  return false;\r\n\r\n String reversed = reverse(input);\r\n return input.equals(reversed);\r\n}\r\n\r\npublic static String reverse(String str) \r\n{ \r\n if (str == null) \r\n  return null;\r\n \r\n if (str.length() &lt;= 1) \r\n  return str;\r\n \r\n return reverse(str.substring(1)) + str.charAt(0);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>String Questions How to find the Longest Substring in String Swapping Letters in 2 Words Print String in Reverse Order Print Left Right Angle Triangle Inverted Right Triangle Print Right Angle Triangle Print Pyramid Sum of Pairs in Array of Numbers Check 2nd List has all elements in 1st List How to find String is&hellip; <a href=\"https:\/\/codethataint.com\/blog\/string-problems\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[278],"tags":[],"class_list":["post-1806","post","type-post","status-publish","format-standard","hentry","category-program-solving"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1806"}],"version-history":[{"count":22,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1806\/revisions"}],"predecessor-version":[{"id":3439,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1806\/revisions\/3439"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}