Advertisement
ogv

Untitled

ogv
Nov 16th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. // Runtime: 1 ms, faster than 100.00% of Java online submissions for Letter Case Permutation.
  2. // Memory Usage: 37.1 MB, less than 100.00% of Java online submissions for Letter Case Permutation.
  3. class Solution {
  4.     public List<String> letterCasePermutation(String S) {
  5.         List<String> results = new ArrayList<String>();
  6.        
  7.         go(S.toCharArray(), 0, new char[S.length()], results);
  8.        
  9.         return results;
  10.     }
  11.    
  12.     private void go(char[] input, int index, char[] output, List<String> results) {
  13.         if (index >= input.length) {
  14.             results.add(new String(output));
  15.             return;
  16.         }
  17.        
  18.         char lower = Character.toLowerCase(input[index]);
  19.         char upper = Character.toUpperCase(input[index]);
  20.        
  21.         output[index] = lower;
  22.         go(input, index + 1, output, results);
  23.        
  24.         if (upper != lower) {
  25.             output[index] = upper;
  26.             go(input, index + 1, output, results);
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement