Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  • Idea
  • Code
  1. Leetcode
  2. Array

Swap Lex Order

PreviousPossible SumNextPartition Equal Subset Sum

Last updated 3 years ago

Given a string str and array of pairs that indicates which indices in the string can be swapped, return the string that results from doing the allowed swaps. You can swap indices any number of times.

Example

For str = "abdc" and pairs = [[1, 4], [3, 4]], the output should be solution(str, pairs) = "dbca".

By swapping the given indices, you get the strings: "cbda", "cbad", "dbac", "dbca". The lexicographically largest string in this list is "dbca".

Idea

Tree structure + connected component + string manipulation

Construct tree for pairs and find connect component to know which indexes can be freely swapped (Ex: 1,4,3 are connected, so any sequence: 143, 134, 341, 314 .... is valid). Each connected component is free to swap and not interfere with each other (Ex: 1,4 and 7,8,9 means 1,4 can freely swap and 7,8,9 can freely swap, but nothing in 1,4 can appear in 7,8,9)

Find the largest lex order of the corresponding connected component (Ex: for 1,4,3-->a,d,c so the largest lex order is dca, which correspond to 431)

Replace the corresponding order to the original string (Ex: original: abdc-->1234, new sequence is 431-->dbca, b is index 2, cannot change)

TreeSet is different from HashSet that TreeSet remain a natural order: set{1,4,3} appears to be {1,3,4} for TreeSet while remain unordered for HashSet

Code

String solution(String str, int[][] pairs) {
    Map<Integer, Set<Integer>> map = new HashMap<>();
    for(int[] pair: pairs){
        int u = pair[0];
        int v = pair[1];
        
        map.putIfAbsent(u, new HashSet<>());
        map.putIfAbsent(v, new HashSet<>());
        
        map.get(u).add(v);
        map.get(v).add(u);
    }
    
    List<TreeSet<Integer>> sccs = new ArrayList<>();
    Set<Integer> visited = new HashSet<>();
    
    for(Integer curr: map.keySet()){
        if(!visited.contains(curr)){
            TreeSet<Integer> scc = new TreeSet();
            scc.add(curr);
            dfs(visited, scc, map, curr);
            sccs.add(scc);
        }
    }
    
    char[] arr = str.toCharArray();
    for(TreeSet<Integer> scc: sccs){
        List<Character> ordered = new ArrayList<>();
        for(int ind: scc){
            ordered.add(arr[ind-1]);
        }
        Collections.sort(ordered);
        Collections.reverse(ordered);
        
        int i = 0;
        for(int ind: scc){
            arr[ind-1] = ordered.get(i);
            i++;
        }
    }
    return String.valueOf(arr);
}

void dfs(Set<Integer> visited, TreeSet<Integer> scc, Map<Integer, Set<Integer>> map, int curr){
    for(int currNeigh: map.get(curr)){
        if(!visited.contains(currNeigh)){
            visited.add(currNeigh);
            scc.add(currNeigh);
            dfs(visited, scc, map, currNeigh);
        }
    }
}
lexicographically largest