Секция алгоритмов
- Задача: Backspace String Compare (leetcode) Given two strings s and t, return true if they are equal when both are typed into empty text editors. ‘#’ means a backspace character.
-
Решение:
// решение через StringBiulder return processString(s).equals(processString(t)); } private static String processString(String str) { StringBuilder result = new StringBuilder(); for (char ch : str.toCharArray()) { if (ch == '#') { if (result.length() > 0) { result.deleteCharAt(result.length() - 1); } } else { result.append(ch); } } return result.toString(); }