[ https://issues.apache.org/jira/browse/GROOVY-11655?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17949761#comment-17949761 ]
ASF GitHub Bot commented on GROOVY-11655: ----------------------------------------- dongwq opened a new pull request, #2215: URL: https://github.com/apache/groovy/pull/2215 see: https://issues.apache.org/jira/browse/GROOVY-11655 > Create extension method to make StringBuilder like a stack > ---------------------------------------------------------- > > Key: GROOVY-11655 > URL: https://issues.apache.org/jira/browse/GROOVY-11655 > Project: Groovy > Issue Type: New Feature > Reporter: John > Priority: Major > > for leetcode problem: > [https://leetcode.com/problems/design-a-text-editor/description/] > the java code below, suppose StringBuilder to be a contain of chars , > meanwhile as a stack. > > {code:java} > class TextEditor { > private final StringBuilder left = new StringBuilder(); > private final StringBuilder right = new StringBuilder(); > public void addText(String text) { > left.append(text); > } > public int deleteText(int k) { > k = Math.min(k, left.length()); > left.setLength(left.length() - k); > return k; > } > public String cursorLeft(int k) { > while (k > 0 && !left.isEmpty()) { > right.append(left.charAt(left.length() - 1)); > left.setLength(left.length() - 1); > k--; > } > return text(); > } > public String cursorRight(int k) { > while (k > 0 && !right.isEmpty()) { > left.append(right.charAt(right.length() - 1)); > right.setLength(right.length() - 1); > k--; > } > return text(); > } > private String text() { > > return left.substring(Math.max(left.length() - 10, 0)); > } > } > {code} > > -- This message was sent by Atlassian Jira (v8.20.10#820010)