Home >Java >javaTutorial >How to remove all consecutive duplicates in a string in Java?

How to remove all consecutive duplicates in a string in Java?

WBOY
WBOYforward
2023-04-24 19:04:131411browse

Algorithm:

One of the more common scenarios for the stack is the operation of strings, such as deduplication, backspacing, paths represented by strings, etc. The operations are often relatively Simple.

1.先把最为条件判断的字符串入栈2.根据新到来的元素判断要不要出栈3.最为比较的元素往往存在栈内,比较的时候,  有时候比较栈顶元素,有时候整个栈都要比较

Question 1: Delete all adjacent duplicates in a string

Code implementation:

func removeDuplicates(S string) string {    ss := []byte(S)    var stacks []byte    for i := 0; i< len(ss);i++ {        if len(stacks) != 0 {            j:= len(stacks)-1            if ss[i] == stacks[j]{ // 与栈顶元素相同的话,删除栈内数据并且也不入栈                stacks = stacks[:j]                continue            }           }         stacks = append(stacks,ss[i])// 其他情况都入栈    }    return string(stacks)}/*栈的使用,先入栈,后面的元素与栈顶元素相同,出栈并且新元素不入栈。其他场景都入栈。*/

Topic 2: Compare strings containing backspaces

Code implementation:

func backspaceCompare(S string, T string) bool {    s := getStack(S)    t := getStack(T)    return s == t}func getStack(S string) string {    ss := []byte(S)    var stacks []byte    for i:=0;i<len(ss);i++{        if ss[i] != &#39;#&#39; {            stacks = append(stacks,ss[i])        } else {            if len(stacks) > 0 { // 注意 ”#abc“这种#在最前面的情况                stacks = stacks[:len(stacks)-1]            }        }    }    return string(stacks)}

Topic 3: Simplify the path

Code implementation:

<code>import "strings"</code><code>func simplifyPath(path string) string {</code><code>    tmps := strings.Split(path,"/")</code><code>    var paths []string</code><code>    for _,s:=range tmps {</code><code>        if len(s) != 0{</code><code>            paths = append(paths,s) </code><code>        }</code><code>    }</code><code>    // fmt.Println("name",paths)</code><code>    var stacks []string</code><code>    for i:=0;i<len(paths);i++{</code><code><br/></code><code>        if paths[i]==".." {</code><code>            if len(stacks) > 0{</code><code>                stacks=stacks[:len(stacks)-1]</code><code>            }</code><code>            continue</code><code>        }</code><code>        if paths[i]!="."{</code><code>            stacks=append(stacks,paths[i])</code><code>        }</code><code>    }</code><code>    // fmt.Println("name",stacks,len(stacks))</code><code>    resStr :=""</code><code>    for idx,s:=range stacks {</code><code>        resStr += s</code><code>        if idx != len(stacks)-1{</code><code>            resStr += "/"</code><code>        }  </code><code>    }</code><code><br></code><code>    return "/"+resStr</code><code>}</code>

The above is the detailed content of How to remove all consecutive duplicates in a string in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete