Convert string to palindrome string with minimum insertions

后端 未结 5 612
心在旅途
心在旅途 2020-12-31 14:42

In order to find the minimal number of insertions required to convert a given string(s) to palindrome I find the longest common subsequence of the string(lcs_string) and its

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 14:43

    Simple. See below :)

            String pattern = "abcdefghgf";
            boolean isPalindrome = false;
            int i=0,j=pattern.length()-1;
            int mismatchCounter = 0;
    
            while(i<=j)
            {
                //reverse matching
                if(pattern.charAt(i)== pattern.charAt(j))
                    {
                        i++; j--; 
                        isPalindrome = true;
                        continue;
                    }
    
                else if(pattern.charAt(i)!= pattern.charAt(j))
                    {
                        i++;
                        mismatchCounter++;
                    }
    
    
            }
            System.out.println("The pattern string is :"+pattern);
            System.out.println("Minimum number of characters required to make this string a palidnrome : "+mismatchCounter);
    

提交回复
热议问题