Tap here to switch tabs
Problem
Submissions

Problem: Shortest Path With K Consecutive Identical Characters

Try to solve the Shortest Path With At Most K Consecutive Identical Characters problem.

Statement

You are given an integer n representing the number of nodes in a directed graph numbered from 00 to n−1n - 1, and a 22D integer array edges where each edges[i] = [ui,vi,wi][u_i, v_i, w_i] denotes a directed edge from node uiu_i to node viv_i with weight wiw_i.

You are also given a string labels of length n, where labels[i] is the character assigned to node ii, and an integer k.

A path from node 00 to node n−1n - 1 is considered valid if the sequence of characters obtained by concatenating the labels of the nodes along the path contains at most k consecutive identical characters.

Return the minimum total weight of a valid path from node 00 to node n−1n - 1. If no such path exists, return −1-1.

Constraints:

  • 1≤1 \leq n ==== labels.length ≤5imes104\leq 5 imes 10^4

  • 0≤0 \leq edges.length ≤5imes104\leq 5 imes 10^4

  • edges[i] ==[ui,vi,wi]== [u_i, v_i, w_i]

  • 0≤ui,vi≤n−10 \leq u_i, v_i \leq n - 1

  • ui≠viu_i \neq v_i

  • 1≤wi≤1041 \leq w_i \leq 10^4

  • labels consists of lowercase English letters.

  • 1≤1 \leq k ≤50\leq 50

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Shortest Path With K Consecutive Identical Characters

Try to solve the Shortest Path With At Most K Consecutive Identical Characters problem.

Statement

You are given an integer n representing the number of nodes in a directed graph numbered from 00 to n−1n - 1, and a 22D integer array edges where each edges[i] = [ui,vi,wi][u_i, v_i, w_i] denotes a directed edge from node uiu_i to node viv_i with weight wiw_i.

You are also given a string labels of length n, where labels[i] is the character assigned to node ii, and an integer k.

A path from node 00 to node n−1n - 1 is considered valid if the sequence of characters obtained by concatenating the labels of the nodes along the path contains at most k consecutive identical characters.

Return the minimum total weight of a valid path from node 00 to node n−1n - 1. If no such path exists, return −1-1.

Constraints:

  • 1≤1 \leq n ==== labels.length ≤5imes104\leq 5 imes 10^4

  • 0≤0 \leq edges.length ≤5imes104\leq 5 imes 10^4

  • edges[i] ==[ui,vi,wi]== [u_i, v_i, w_i]

  • 0≤ui,vi≤n−10 \leq u_i, v_i \leq n - 1

  • ui≠viu_i \neq v_i

  • 1≤wi≤1041 \leq w_i \leq 10^4

  • labels consists of lowercase English letters.

  • 1≤1 \leq k ≤50\leq 50