import javax.swing.*; public class ProblemF { public static void main(String[] args) { String W = JOptionPane.showInputDialog("Enter W"); int[] lengthOfLongestSubseq = new int[W.length()]; for (int i = 0; i < W.length(); ++i) { lengthOfLongestSubseq[i] = 1; int tempMax = 1; for (int j = 0; j < i; ++j) { if (W.charAt(j) <= W.charAt(i)) { if (lengthOfLongestSubseq[j]+1 > tempMax) { tempMax = lengthOfLongestSubseq[j] + 1; } } } lengthOfLongestSubseq[i] = tempMax; } int longestLength = 0; for (int i = 0; i < W.length(); ++i) { if (lengthOfLongestSubseq[i] > longestLength) { longestLength = lengthOfLongestSubseq[i]; } } JOptionPane.showMessageDialog(null, "The longest special word has length " + longestLength); } }